Something you must know about the AlarmManager class

If you already worked on a few Android applications, you probably also used the AlarmManager class to schedule alarms (like pending notifications). This all seems pretty straight forward, and it is, but there is a small detail you should know about and never forget when scheduling alarms.

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);

// setRepeating() lets you specify a precise custom interval--in this case 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 20, alarmIntent);

There is a small tutorial about scheduling alarms on the training section of the Android developer website. You should definitely read the paragraph about ‘Start an Alarm When the Device Boots‘:

By default, all alarms are canceled when a device shuts down. To prevent this from happening, you can design your application to automatically restart a repeating alarm if the user reboots the device. This ensures that the AlarmManager will continue doing its task without the user needing to manually restart the alarm.

You can solve this issue by following three simple steps:

1. Set the RECEIVE_BOOT_COMPLETED permission in your application’s manifest. This allows your app to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting (this only works if the app has already been launched by the user at least once):

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

2. Implement a BroadcastReceiver to receive the broadcast:

public class SampleBootReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            // Set the alarm here.

3. Add the receiver to your app’s manifest file with an intent filter that filters on the ACTION_BOOT_COMPLETED action:

<receiver android:name=".SampleBootReceiver"
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>

To see which alarms are set on your device, you can dump them to your console with ADB:

adb shell dumpsys alarm

You can follow this small tutorial in more detail on