You probably forget things from time to time. It’s only human. The Android
AlarmManager is no different. The
AlarmManager doesn’t persist alarms; therefore, when the device reboots, you must set up the alarms again.
Creating a boot receiver
RECEIVE_BOOT_COMPLETED permission allows your application to receive a broadcast notification from Android when the device is done booting and is eligible to be interactive with the user. Because the Android system can broadcast a message when this event is complete, you need to add another
BroadcastReceiver to your project. This
BroadcastReceiver is responsible for handling the boot notification from Android. When the broadcast is received, the receiver needs to retrieve the tasks from the
ReminderProvider and loop through each task and schedule an alarm for it, to ensure that your alarms don’t get lost in the reboot.
Add a new
BroadcastReceiver to your application. For the Task Reminder application, the
BroadcaseReceiver has the name
OnBootReceiver. You also need to add the following lines of code to the application element in the
<receiver android:name=”.OnBootReceiver” android:exported=”false”>
<action android:name=”android.intent.action.BOOT_COMPLETED” />