I'm using AlarmManager to set up repeating intents but it has caused some little troubles so hope anyone could help.
Summary
There are 2 pending intents. One runs at 1000 and another runs at 2000 every day. Each contains a row id from the database for identification purpose. The code looks something like this:
Intent i = new Intent(mContext, ScheduleReceiver.class);
i.putExtra(RuleDBAdapter.KEY_ROWID, (int)taskId);
PendingIntent pi =PendingIntent.getBroadcast(...);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
Deletion:
The problem is when we need to delete one of them. The proper way to delete a pending intent is to set up an identical one and then call cancel from AlarmManager.
Android Documentation:
public void cancel (PendingIntent operation)
Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent matches this one (as defined by filterEquals(Intent)), will be canceled.public boolean filterEquals (Intent other)
Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.
So in the above example, if I make an identical intent then cancel, both of the above intents will get canceled, because they are from the same class/same action etc (except the "extra" data is rowId but filterEquals
doesn't care about extra data).
Is there any workaround for this?
You could try using the requestCode parameter when creating your pending intent:
PendingIntent pi =PendingIntent.getBroadcast(mContext, yourUniqueDatabaseId,i,PendingIntent.FLAG_ONE_SHOT);
This should create an Intent that is unique for matching purposes..
public boolean filterEquals(Intent other) compare the action, data, type, package, component, and categories, but do not compare the extra, so the difference between extras cannot distinguish Intents. Try to set different data to these Intents, the data could be insignificant Uris.
Try to read the source code to get more infomation.
Trying putting a dummy action in the intent. Eg.
myIntent.setAction(""+System.currentTimeMillis());
This will cause filterEquals to return false.
I needed a similar solution for sending sms notifications from multiple numbers, in order to allow the notification to startup the sms thread viewer with different numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With