AlarmManager.setRepeating idempotent?
In my Android app, I am setting an alarm that I want to repeat and hence using AlarmManager.setRepeating ().
I don't want to track if an alarm is set (sounds like a bad idea prone to crashing at some point) and there doesn't seem to be any API support to check if a specific alarm is set for a given intent.
Hence, I pessimistically reset my alarm every time my app is activated:
alarmManager.cancel(pendingIntent);
...
alarmManager.setRepeating(..., pendingIntent);
Question: calls setRepeating () idempotent, i.e. do I need to explicitly undo () any prior alarm or can I just call setRepeating () and be done with it?
source to share
If an alarm has already been set for the same IntentSender, this will be canceled first.
I know this is not entirely clear, but they say they AlarmManager
will consider PendingIntent
which one you go to setRepeating()
and if an alarm with an identical one is already scheduled PendingIntent
, then that alarm will be canceled before a new one is scheduled. So you don't need to cancel any previous alarms until the PendingIntent you are using changes(oldPendingIntent.equals(newPendingIntent) == true)
From the PendingIntent API Documentation :
equals(Object otherObj)
Comparison operator on two PendingIntent objects, such that true is returned then they both represent the same operation from the same package.
source to share