我有2个BroadcastReceivers和2个意图,我想单击一个按钮,然后在5m之后开始广播1,然后在10m之后开始广播2,发生的事情是它们都在我单击10m后开始,我的猜测是,意图不是唯一的,为每个设置不同的reqeustCode。

按钮的OnClick:

    Bundle bd = new Bundle();
    bd.putInt("mInt", i);

    Intent intent1 = new Intent(getActivity(), Broadcast_1.class);
    intent1.putExtras(bd);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getActivity().getApplicationContext(), i, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager1 = (AlarmManager) getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmManager1.setRepeating(AlarmManager.RTC, System.currentTimeMillis()+1000*60*5, 1000*60*10, pendingIntent1);
    Toast.makeText(getActivity(), "countdown started "+i ,Toast.LENGTH_SHORT).show();

    Intent intent2 = new Intent(getActivity(), Broadcast_1.class);
    intent2.putExtras(bd);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getActivity().getApplicationContext(), i+42212342, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager2 = (AlarmManager) getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmManager2.setRepeating(AlarmManager.RTC, System.currentTimeMillis()+1000*60*10, 1000*60*10, pendingIntent2);

BroadcastReceiver_1和_2(它们看起来相同)类:
public class Broadcast_1 extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Calendar c = Calendar.getInstance();
        int seconds = c.get(Calendar.SECOND);
        int minutes = c.get(Calendar.MINUTE);
        ShowTextFragment.setText("Broadcast_1" + " at " + minutes + " : " + seconds);

    }

}

问题:
为什么最近的意图将先前的意图推到他的开始时间?

我通过打印广播代码执行时间确认了此行为。请帮忙

最佳答案

您看到的问题是repeating alarms now work的方式。为了延长电池寿命,现在AlarmManager在重新安排警报以将多个警报分组在一起方面具有很大的自由度。基本上,如果您需要任何准确的时间安排,则应该忘记使用setRepeating()。请改用setExact()。如果您需要重复的闹钟,只需在闹钟响起时将其重置即可。

10-06 06:51