Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create different pendingintent so filterEquals() return false?

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?

like image 588
markbse Avatar asked Sep 21 '11 08:09

markbse


3 Answers

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..

like image 64
Jer Avatar answered Nov 02 '22 15:11

Jer


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.

like image 22
faylon Avatar answered Nov 02 '22 15:11

faylon


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.

like image 20
guyland123 Avatar answered Nov 02 '22 13:11

guyland123