问题
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?
回答1:
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..
回答2:
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.
回答3:
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.
回答4:
Put the task id and a timestamp into the action of the intent. That way, you associate the pending intent with the task id and the intents will still be different in terms of filtering.
intent.setAction("ACTION_" + taskId + "_" + currentTimestamp);
回答5:
In my case I didn't have a good way to generate a unique key for my pending intents so what I did was add custom data to the generated intent.
Once you start filling the data attribute, you have to remember to add a data parameter to the intent-filter in the android manifest. If you don't do this it will not trigger your custom receiver.
Examples:
intent
Intent intent = new Intent(reminderFire);
intent.addCategory(reminderCategory);
Uri.Builder builder = new Uri.Builder();
builder.scheme(context.getString(R.string.scheme));
builder.authority(context.getString(R.string.intent_filter_reminder_host));
builder.appendQueryParameter("oid", Reminder.getOid());
intent.setData(builder.build());
AndroidManifest entry
<receiver
android:name=".client.MyReminder"
android:enabled="true"
>
<intent-filter>
<action android:name="com.example.client.reminder.fire"/>
<action android:name="com.example.client.reminder.category"/>
<data
android:host="@string/intent_filter_reminder_host"
android:scheme="@string/scheme" />
</intent-filter>
</receiver>
By doing this you can also test uniqueness in the intent before it gets added as a pending intent, which is useful if you're unit testing.
来源:https://stackoverflow.com/questions/7496603/how-to-create-different-pendingintent-so-filterequals-return-false