entertainment

I Stimuli, your entertainment hub for movies, music and books.

Search The Movie Database and The Open Movie Database for movies and TV shows.

Watch movies on Amazon and Youtube.

Search Spotify for music. Play music on Spotify.

Buy books, music and movies on Amazon.

Scheduling Android's Repeating Alarms

  • Written by  Clive

All about repeating, I said repeating, Alarms

Android repeat alarm icon

You can use alarms to trigger events at any time even if your app is not running.

Alarms:

  • Enable you to trigger events at set times and/or intervals
  • Can be used together with receivers to start services and other operations
  • Operate outside of your app so they can trigger events even if your app is not running - even if the device is asleep
  • Help you to minimise your app’s resources. Let the alarm take care of it

Best practices when using repeating alarms

  • It’s best to let your repeat alarm fire randomly when triggering network requests. You don’t want your million plus apps all hitting the server at the same time!
  • Try to use setInexactRepeating() instead of setRepeating() when setting your repeat alarms  – Android will then synchronise all the of the device’s repeating alarms to fire at the same time. This saves the battery by only having to wake the device once
  • It’s all about not draining the battery, so keep the repeat alarm frequency to a minimum
  • Again, watch the battery usage, don’t wake the device unnecessarily
  • Try to avoid basing the alarm trigger on clock time if possible, rather use ELAPSED_REALTIME instead

There are two types of repeating alarms

  • Elapsed real time – these trigger after a set period of time. They’re suitable for repeat alarms based on the passage of time – it’s not affected by the time-zone/locale. There are two versions:
    • ELAPSED_REALTIME – triggers after a specified period of time. Doesn’t wake up the device
    • ELAPSED_REALTIME_WAKEUP – same as above but wakes the device up
  • Real clock time (RTC) – uses UTC time. Best suited for alarms dependent on the current locale. There are two versions:
    • RTC - fires on a specified clock time. Doesn’t wake up the device
    • RTC_WAKEUP – same as above but wakes the device up

All repeating alarms not using the wake up version will fire as soon as the device wakes up.

Setting your repeating alarm

Repeating alarms are ideal for triggering regular events.

The alarm is an Intent broadcast to a receiver which we must register.

They’re easy to set, just get an instance of an AlarmManager and call one of the following methods:

  • set – schedules an alarm to trigger at a specific time or after a specific time period. Pass it three parameters:
    • the type of alarm
    • the time in milliseconds that the alarm should trigger
    • action to perform when the alarm triggers
  • setInexactRepeating – schedules a repeating alarm that has an inexact trigger time. The system can adjust the triggering time to group alarms to fire together. From API 19 and up, all repeating alarms are inexact. Pass it four parameters:
    • the type of alarm
    • the time in milliseconds that the alarm should first trigger. It will not be exact
    • the interval, in milliseconds, between subsequent triggers. You must use one of the following:
      • INTERVAL_DAY
      • INTERVAL_FIFTEEN_MINUTES
      • INTERVAL_HALF_DAY
      • INTERVAL_HALF_HOUR
      • INTERVAL_HOUR
    • action to perform when the alarm triggers
  • setRepeating – schedules a repeating alarm. Similar to the set() method but you include a time interval after which the alarm triggers repeatedly. Pass it four parameters:
    • the type of alarm
    • the time in milliseconds that the alarm should first trigger
    • the interval, in milliseconds, between subsequent triggers
    • action to perform when the alarm triggers

Cancelling your repeating alarm

You can cancel the alarm at any time. Simply call cancel(), passing it the same pending intent that you used to trigger the alarm.

Restart your repeating alarm when the device boots

All alarms are cancelled when the device shuts down.

If you need to, you can force the system to restart your repeating alarms when the device reboots. Check out the official documentation on how to do that.

The official documentation also has a great example app that uses a repeat alarm to trigger an IntentService to do some work. It uses a wake lock and also demonstrates how to re-start a repeating alarm after the device reboots. You can download the sample app here.

Our tutorial app

Our tutorial app shows you how to use a repeating alarm to fire repeatedly as well as once-off.

The main activity displays two buttons.

Pressing the One Time Alarm button sets a one-time alarm to trigger after 5 seconds. Pressing the Repeat Alarm button sets an inexact repeating alarm.

When the alarms are triggered, they broadcast their intents which are received by our AlarmReceiver. This is where you would normally start a Service to perform some task but we simply send a notification.

Touching the notification returns the user to the main activity where a cancel button is displayed. Pressing the Cancel Alarms button cancels the alarms.

The MainActivity Activity

We get an instance of the AlarmManager in the activity’s onCreate() method. We’ll use this alarm manager instance for both of our alarms.

We also create an intent and a pending intent.

Here’s the important part of the code:

Android repeat alarm alarmManager

Get an instance of the AlarmManager and create the PendingIntent

Note the following:

  • alarmManager – our instance of an AlarmManager. It gives us access to the system alarm services. We’ll use this instance of the alarm manager in both of our alarms
  • alarmIntent – our broadcast intent that will be broadcast to the AlarmReceiver when the alarm is triggered
  • pendingIntent – this will be passed on to the alarm manager’s set methods when we set the alarms
  • getBroadcast - retrieves a pending intent that will perform a broadcast. It’s like calling sendBroadcast(). There are four parameters:
    • the context
    • our unique request code
    • an intent. It should be an explicit intent for security reasons
    • flags. We don’t use it so we pass 0

Firing the one-time alarm

Pressing the One Time Alarm button sets a one-time alarm that fires after 5 seconds.

Here’s the code:

Android repeat alarm RTC_WAKUP one-Time alarm

We use the AlarmManager’s set() method, passing it the type of alarm, when to trigger it and the pending intent as parameters

Note the following:

  • alarmType – we’re using the “real time clock” to trigger the alarm at a specific “clock time”. We’re using the wakeup version to wake the device at the specified time
  • whenToTrigger – the “clock time” when the alarm will trigger. We use the Calendar now time plus 5 seconds as our triggering time
  • set – schedules the alarm to fire at the specified time. We pass it three parameters:
    • the alarm type
    • the clock time when the alarm should trigger
    • the action to perform when the alarm triggers

Firing the repeat alarm

Pressing the Repeat Alarm button sets an alarm that fires after fifteen minutes and then repeatedly after every fifteen minutes.

Here’s the code:

Android repeat alarm setInexactRepeating alarm

You must use one of the interval constants when using the setInexactRepeating() methods

Note the following:

  • alarmType – we’re using the ELAPSED_REALTIME_WAKEUP alarm type. The alarm will trigger after the specified time.  We’re using the wakeup version to wake the device at the specified time
  • triggerTime – the time period after which the alarm should trigger for the first time
  • intervalTime – the time interval between repeated firings of the alarm
  • setInexactRepeating – schedules the alarm to fire at the specified times. We pass it four parameters:
    • the alarm type
    • the time period after which the alarm should first be triggered
    • the time interval between repeated firings of the alarm
    • the action to perform when the alarm triggers

Cancelling the alarms

Pressing the Cancel Alarms button executes the cancelAlarms() method:

Android repeat alarm alarm cancel

Pressing the button cancels all alarms using the same pending intent and then hides the button

Note the following:

  • setVisibility – we set the cancel button to be invisible
  • if there is an instance of the alarm manager, we cancel it by calling cancel(), passing it the pending intent that was used when setting the alarm

The Receiver

Our receiver, AlarmReceiver receives the broadcast intent when the alarm is triggered.

Don’t forget to register the receiver in the manifest.

This is where you’d normally do the work or start a Service. We simply send a notification each time an alarm is triggered.

You can read all about notification in our You can master notifications series of tutorials. Also, check out Android Broadcast Receivers: A tutorial for more on receivers.

Touching the notification takes the user back to the main activity where a cancel button is made visible. The user can then cancel the alarm.

I hope that you have found this tutorial helpful.

This project was created using Android Studio.

You can download the project files here Download icon

Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files.