Lisa Anne
Lisa Anne

Reputation: 4595

How to cancel alarms?

I have the following situation.

I need to set many alarms to start the same Activity at different specific dates.

In order to accomplish this, I obviously use an AlarmManager.

Since the PendingIntents given to the AlarmManager are all equivalent, in order to have Android create all of them (and not just 1) I use a different requestCode to differentate among them.

All of this works fine, all alarms are created correctly.

The point is that sometimes I have to cancel them!

If I do not do it with the same requestCode the AlarmManager does not cancel them.

It is very difficult to retrieve the original requestCode in the code, since they are created at runtime at different moments/days...

Any suggestion on how to address this situation?

Upvotes: 1

Views: 159

Answers (1)

kabuto178
kabuto178

Reputation: 3167

You have the right approach, as the only way I know to cancel the alarms would be cancel the pending intents with the same requestCode, however your issue is retrieving the same request codes made at run time. You would have to store these codes some how either by SQLite, or shared preferences perhaps to have them stored on the device then retrieve them as needed. Alternatively you could pass the requestCode as a bundle in the intent and then cancel it immediately after it fires off, or when ever through the alarms life cycle you choose. Hope this helped a bit.

Upvotes: 1

Related Questions