Multiple calls to AlarmManager.setRepeating deliver the same Intent/PendingIntent extra values, but I supplied different ones

痞子三分冷 提交于 2019-11-27 05:43:05

问题


Solved while writing this question, but posting in case it helps anyone:

I'm setting multiple alarms like this, with different values of id:

AlarmManager alarms = (AlarmManager)context.getSystemService(
        Context.ALARM_SERVICE);
Intent i = new Intent(MyReceiver.ACTION_ALARM);  // "com.example.ALARM"
i.putExtra(MyReceiver.EXTRA_ID, id);  // "com.example.ID", 2
PendingIntent p = PendingIntent.getBroadcast(context, 0, i, 0);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, nextMillis, 300000, p);  // 5 mins

...and receiving them like this:

public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION_ALARM)) {
        // It's time to sound/show an alarm
        final long id = intent.getLongExtra(EXTRA_ID, -1);

The alarm is delivered to my receiver at the right times, but often with EXTRA_ID set to the wrong value: it's a value that I have used at some point, just not the one that I wanted delivered at that particular time.


回答1:


The documentation for PendingIntent.getBroadcast() says:

Returns

Returns an existing or new PendingIntent matching the given parameters.

The problem is that two Intents differing only in extras seem to match for this purpose. So getBroadcast() will return some random old PendingIntent (with a different EXTRA_ID) instead of a new one around the Intent I just created. The fix is to supply a data Uri and make it differ with the id, like this:

Intent i = new Intent(MyReceiver.ACTION_ALARM, Uri.parse("timer:"+id));

You can then retrieve the id number using:

Long.parseLong(intent.getData().getSchemeSpecificPart());

...or of course supply the extra as well and use that.




回答2:


You could also use the flag PendingIntent.FLAG_UPDATE_CURRENT

PendingIntent p = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

this should the work too




回答3:


The solution for your problem is use Intent.FLAG_ACTIVITY_NEW_TASK

  p = PendingIntent.getBroadcast(context, 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);


来源:https://stackoverflow.com/questions/2844274/multiple-calls-to-alarmmanager-setrepeating-deliver-the-same-intent-pendinginten

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!