2

Simple question: is the AlarmManager always cleared after reboot? Is it cleared after reboot on all devices and even when the user boots his device up very shortly after he booted it off?

I need to know this because I recreate my app's alarm in an OnBootReceiver and I want to avoid having double alarms set.

Xander
  • 5,487
  • 14
  • 49
  • 77

1 Answers1

6

is the AlarmManager always cleared after reboot?

Definitely on a full reboot. There are some devices that have a "quickboot" (HTC comes to mind), and I have not run experiments to see what the behavior is there.

and even when the user boots his device up very shortly after he booted it off?

Yes.

I need to know this because I recreate my app's alarm in an OnBootReceiver and I want to avoid having double alarms set.

Alarms are effectively in a HashMap keyed by PendingIntent. Setting an alarm using an equivalent PendingIntent should replace the prior alarm. By "equivalent", I mean:

  • Same operation (activity, service, broadcast)
  • Same request code
  • Equivalent Intent (matches via filterEquals(), which pretty much means it matches on everything except the extras)
  • And I'd be nervous about using FLAG_CANCEL_CURRENT when defining the new PendingIntent

You can use adb shell dumpsys alarm to confirm what alarms are set, so you are sure that you wind up with the right number of alarms.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So would it perhaps be a good idea to call `cancel()` first in the OnBootReceiver and after that call `set()`? – Xander May 04 '13 at 13:43
  • @Merlin: Personally, I am not aware of any scenario in which that is required. For example, the HTC quickboot does not trigger `ACTION_BOOT_COMPLETED`. That being said, you are certainly welcome to do so -- `cancel()` should not have an issue if there is no existing alarm for that `PendingIntent`. – CommonsWare May 04 '13 at 13:49
  • Yeah well, my OnBootReceiver receives `android.intent.action.QUICKBOOT_POWERON` as well (in case the alarms are cancelled in quickboot), so I'll just cancel it before setting it. Thank you for your help! – Xander May 04 '13 at 14:06