Search code examples
androidbroadcastreceiveralarmmanageralarmboot

AlarmManager with BroadcastReceiver - how is it meant to work?


I'm trying to make a task schedule app and I made an Alarm app trying to learn how to do that part at least. It uses AlarmManager and it makes an alarm go off at a time chosen by a TimePicker. But it doesn't work when the emulator is turned off and on again.

So I'm trying to use BroadcastReceiver but I don't understand any of the guides...I mean am I supposed to set the intent that the alarm manager does to the BroadcastReciever? Or can I just start up the app and then the alarm exists again or what? How are the alarms stored in android?


Solution

  • But it doesn't work when the emulator is turned off and on again.

    That is the correct behavior -- AlarmManager's schedule is cleared on a reboot. You need to specifically register to receive the ACTION_BOOT_COMPLETED broadcast, in order to re-establish your alarm events after a reboot.

    I mean am I supposed to set the intent that the alarm manager does to the BroadcastReciever?

    Well, if you are using a _WAKEUP-style alarm, the recipe is to use a getBroadcast() PendingIntent with AlarmManager, where the BroadcastReceiver is either a WakefulBroadcastReceiver (and follows those instructions) or passes control to my WakefulIntentService.

    I have somewhat-contrived examples of using WakefulIntentService and WakefulBroadcastRecevier.

    How are the alarms stored in android?

    AFAIK, they are held in the memory of a core OS process and are not persisted.