Fred Kerr
Fred Kerr

Reputation: 47

How do you cancel a specific alarm manager after app is restarted?

I'm creating an app that, after receiving a text from a certain number, starts a repeating alarm using AlarmManager. The AlarmReciever plays an alarm sound for thirty seconds and then the alarm repeats every five minutes. I want to cancel the AlarmManager when the app is closed and restarted by the user but I have to use the same instance of the alarmIntent to cancel it.

Upvotes: -1

Views: 263

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007322

I have to use the same instance of the alarmIntent to cancel it.

No, you have to use an equivalent PendingIntent to cancel it. By "equivalent", I mean:

  • It is the same operation (e.g., activity, service, broadcast)
  • It has the same request code (2nd parameter to methods like getActivity())
  • It has an equivalent Intent

By "equivalent Intent", I mean that all the routing information is the same (component, action, data, MIME type, categories). Extras do not matter.

You need to hold onto enough information in a persistent data store (e.g., file) to be able to create an equivalent PendingIntent to pass to cancel() on AlarmManager.

Upvotes: 3

Related Questions