Android guide to setting a single or repeating alarm

If you are developing an app which deals with alarms, you might be confused as to how to set either a single alarm or a repeating alarm, I will walk you through it so you can get a better understanding of how it works.

In this example, we will say we are trying to create an alarm that triggers some piece of code at 8:30 AM, either once or repeating everyday.
Use this code snippet wherever it is that you want to create your alarm.

AlarmManager alarmMgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
Intent receiverIntent = new Intent(c, WakeupReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(c, 0, receiverIntent, 0); //The second parameter is unique to this PendingIntent,
                                                                                 //if you want to make more alarms, 
                                                                                 //make sure to change the 0 to another integer
int hour = 8;
int minute = 30;

Calendar alarmCalendarTime = Calendar.getInstance(); //Convert to a Calendar instance to be able to get the time in milliseconds to trigger the alarm
alarmCalendarTime.set(Calendar.HOUR_OF_DAY, hour);
alarmCalendarTime.set(Calendar.MINUTE, minute);
alarmCalendarTime.set(Calendar.SECOND, 0); //Must be set to 0 to start the alarm right when the minute hits 30

//Add a day if alarm is set for before current time, so the alarm is triggered the next day
if (alarmCalendarTime.before(Calendar.getInstance())) {
   alarmCalendarTime.add(Calendar.DAY_OF_MONTH, 1);
}

alarmMgr.set(AlarmManager.RTC_WAKEUP, alarmCalendarTime.getTimeInMillis(), alarmIntent);

If you want to set an alarm to be repeating everyday at 8:30 AM, change the last line to:

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, alarmCalendarTime.getTimeInMillis(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS), alarmIntent);

Now create a new java class called WakeupReceiver which extends BroadcastReceiver, this is where the code will be executed when the alarm is triggered.

Don’t forget to register it in your AndroidManifest.xml file  in the <application> tag like so:

<receiver
    android:name=".bedside.WakeupReceiver"
    android:enabled="true"
    android:exported="true"
    android:process=":remote" />

Your WakeupReceiver class should look like

public class WakeupReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(final Context context, Intent intent) {
        // TODO code executed on alarm trigger
    }
}

To cancel any alarm before it is triggered, simply call .cancel(pendingIntent) on the alarmMgr object

alarmMgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
receiverIntent = new Intent(c, WakeupReceiver.class);
alarmIntent = PendingIntent.getBroadcast(c, 0, receiverIntent, 0); //Remember that the second
                                                                   //parameter is unique to the alarm
alarmMgr.cancel(alarmIntent);

And there you have it! You can now easily set a single or repeating alarm in Android!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>