2

I am currently developing an android application where I need to setup persistent alarms that will fire on a specific date and time and show a notification. It works well but the system clears all alarms upon rebooting.

From other questions I know that if I create a BroadcastReceiver forBOOT_COMPLETED I can rearm canceled alarms. My question is: What information about those alarms I need to keep in order to rearm them when needed?

Some people say that I need to persist all the Intent extras and the fire datetime in order to recreate the PendingIntent upon rebooting.

Others say that if I only persist the requestCode for the PendingIntent, after reboot I can use this code to get the canceled PendingIntent and rearm the alarm, because when the device reboots the PendingIntent's are just canceled instead of deleted.

Which one is right way to do it?

Shashanth
  • 4,151
  • 7
  • 32
  • 46
Arthur91BR
  • 23
  • 2
  • 2
    "What information about those alarms I need to keep in order to rearm them when needed?" -- the same information that you are using to schedule the alarms in the first place. "Others say that if I only persist the requestCode for the Pending Intent, after reboot I can use this code to get the canceled Pending Intent and rearm the alarm, because when the device reboots the Pending Intents are just canceled instead of deleted" -- in a word, no. AFAIK, a `PendingIntent` does not survive a reboot. – CommonsWare Dec 02 '16 at 22:32

1 Answers1

0

PendingIntents will not persist after reboot, so to be safe just restart your alarms in the BroadcastReceiver with all the intent extras that you make when you first initialized the alarm, and keep the requestcode the same.

Nick Friskel
  • 1,773
  • 2
  • 16
  • 30
  • Also if you are curious about what the request code does please check out http://stackoverflow.com/questions/21526319/whats-requestcode-used-for-on-pendingintent – Nick Friskel Dec 06 '16 at 00:30
  • Can you explain what you mean in more detail when you say restart your alarms in BroadcastReceiver "with all of the intent extras that you make when you first initialized the alarm"? How do I keep those intent extras to use them again so the boot alarm knows that it's the same alarm? I appreciate your help. – Stark Apr 21 '19 at 10:41
  • You both are going good, but it will work on emulators only not on the real world devices. Developers & their hard work has no values in real world & in on real world devices – sandhya sasane Apr 22 '19 at 16:31