Cancelación de PendingIntents específicos con AlarmManager

I am scheduling multiple intents to be send to a BroadcastReceiver. The difference between every intent is the time when it will be received and extra data it contains.

Now I need to allow user to cancel specific alarms. According to AlarmManager.cancel() javadoc, it compares intents using filterEquals method:

http://developer.android.com/reference/android/content/Intent.html#filterEquals(android.content.Intent)

Now the problem is that according to filterEquals, all my intents are the same. Method checks against "action, data, type, class, and categories".

And unique data is either in intent extra bundle or as a time in AlarmManager. I tried changing any of these parameters - for example using:

intent.setData(Uri.withAppendedPath(Uri.EMPTY, "some_unique_data"));

That would make every intent unique according to filterEquals method, but after changing Data to something like this, my intent does not reach BroadcastReceiver anymore.

What specific bit of data can I change to make every intent unique according to filterEquals and still receivable to my simple BroadcastReceiver?

Maybe I can modify my receiver in any way?

preguntado el 09 de marzo de 12 a las 15:03

There is a parameter inside PendingIntent.getBroadcast method called requestCode, but as it's documentation states: "requestCode Private request code for the sender (currently not used).". Does that mean, that this code is not used in any way, or can I specify this as a unique ID for canceling pending intents? -

Can you please accept your answer. That will remove this question from the list of open questions, and maybe help someone else with a similar problem. Thanks. -

2 Respuestas

Ok, so I figured it out. As my every alarm has unique time and object ID, this is how I create my PendingIntent:

return PendingIntent.getBroadcast(context, time*100000+o.getId(), toFire, 0);

Now time is either 24 or 15 (24 hours before or 15 minutes before event I fire my alarm) and alarm data object contains unique ID. So I add both and specify it as a "requestCode" parameter in getBroadcast. That is enough to make unique intents based on same data. Hope this helps anyone who has the same problem!

respondido 10 mar '12, 14:03

I ran across a similar problem for my app. In my app, I used the SQLite DB to store details about each alarm, along with a unique ROW_ID.

So in my alarm listview, each row had a corresponding ROW_ID associated with it. If the user clicks on a particular row, I just pass this ROW_ID to the method that performs the delete operation

Now, all my alarms were created with Intents which had their data part set as:

Uri.parse(Constants.INTENT_PRE + ROW_ID);

so in order to delete them, I create an Intent that has same data (as above, along with the unique ROW_ID) that was used while creating it.

Needless to say, from "action, data, type, class, and categories", only the "data" part is different for each of my existing PendingIntents.

¡Espero que esto ayude!

respondido 09 mar '12, 15:03

Hey. Well, I don't know what you have in your Constants, but in mine there is no INTENT_PRE and setting Data to for example Uri.parse(Constants.CONTENT_DIRECTORY + o.getId()) still makes it not receivable to broadcastReceiver. - Kostas

Constants is just my own Class where I store some of my String constants which don't need to be translated or are not part of the UI. - Zeiger

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.