Resetting alarms after a system reboot
The AlarmManager
service is a convenient class to schedule working on your Android application; however, when the device shuts down or reboots, all your alarms will be lost since the system does not retain them between system restarts.
To reset the alarm, we should persist your alarms and create a BroadcastReceiver
that sets our alarms whenever a system boot happens:
public class BootBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Retrieve the persisted alarms List<SMSSchedule> persistedAlarms = getStoredSchedules(); // Set again the alarms ... } List<SMSSchedule> getStoredSchedules() {...} }
In order to store our alarms, we created a POJO
class SMSSchedule
as the model for our schedules.
Second, in the Android Manifest we have to register our BroadcastReceiver
to receive the boot event:
<receiver android:name=".chapter6.BootBroadcastReceiver" ...