Rebooting Devices

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.

warning_bomb.eps If you don’t set up your alarms again, they simply don’t fire, because, to Android, they don’t exist.

Creating a boot receiver

The 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 AndroidManifest.xml file:

<receiver android:name=”.OnBootReceiver” android:exported=”false”>

<intent-filter>

<action android:name=”android.intent.action.BOOT_COMPLETED” />

</intent-filter> ...

Get Android Application Development For Dummies, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.