Triggering Alarms Tutorial for Android: Getting Started
Learn how to set up alarms in your Android apps using the AlarmManager API, and find out about the exact and inexact alarm types as well as best practices. By Denis Buketa.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Triggering Alarms Tutorial for Android: Getting Started
25 mins
- Getting Started
- Triggering an Exact Alarm
- Defining an AlarmManager Instance
- Declaring a Permission
- Checking Your App’s Permissions
- Opening the Alarms & Reminders Screen
- Handling App Permission
- Defining an Alarm Action
- Scheduling an Alarm
- Understanding Alarm Types
- Canceling an Alarm
- Triggering an Inexact Alarm
- Defining Alarm Actions
- Scheduling an Alarm After a Specific Time
- Scheduling an Alarm During a Time Window
- Scheduling a Repeating Alarm
- Rescheduling an Alarm When the Device Restarts
- Exploring Best Practices
- Considering System Resource Consumption
- Understanding Acceptable Use Cases For an Exact Alarm
- Using Alarms For Network Requests
- Alarm Time Precision
- Avoid Basing Your Alarm on Clock Time
- Where to Go From Here?
Rescheduling an Alarm When the Device Restarts
By default, the system cancels all alarms when a device shuts down. To prevent this, improve your app by rescheduling alarms when the user reboots the device.
Open AndroidManifest.xml, and replace TODO (22) with:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
When the system finishes booting, it broadcasts ACTION_BOOT_COMPLETED
. This allows your app to receive that event and react to it. Keep in mind that this only works if the user has already launched the app at least once.
Next, open RescheduleAlarmsBroadcastReceiver.kt, and replace TODO (23) with:
class RescheduleAlarmsBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (action != null) {
if (action == Intent.ACTION_BOOT_COMPLETED) {
(context.applicationContext as StuddyApplication).exactAlarms.rescheduleAlarm()
(context.applicationContext as StuddyApplication).inexactAlarms.rescheduleAlarms()
}
}
}
}
This adds a whole new BroadcastReceiver
, which will react on ACTION_BOOT_COMPLETED
events.
Finally, go back to AndroidManifest.xml. Replace TODO (24) with:
<receiver
android:name="com.yourcompany.android.studdy.alarm.RescheduleAlarmsBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
With this code, you define and register the BroadcastReceiver
in the AndroidManifest.xml. It’ll reschedule your alarms after the system finishes booting.
Now, if you schedule any alarm, and restart the device, the alarms are still scheduled.
Exploring Best Practices
Now that you’ve completed the app, you’ll go over some of the best practices when working with alarms.
Considering System Resource Consumption
Triggering exact alarms is expensive. When the system triggers exact alarms, the device consumes a lot of resources, such as battery charge, especially if it’s in a power-saving mode. Furthermore, the system can’t easily batch these requests to use resources more efficiently. When creating an exact alarm, always consider using an inexact alarm instead.
Understanding Acceptable Use Cases For an Exact Alarm
Use exact alarms only when your app has user-facing functionalities that require a specific action to be invoked at a precise time. For example:
- An alarm clock app or a timer app.
- A calendar app that shows notifications for upcoming events.
Using Alarms For Network Requests
If you plan to trigger network requests depending on a repeating alarm, add some randomness to when they’re called. If every instance of your app on multiple devices pings the server at the same time, your server might not be able to handle the load.
Alarm Time Precision
When specifying the time for alarms, don’t make it any more precise than necessary. Always think about how precise your app needs to be when working on a specific use case.
Avoid Basing Your Alarm on Clock Time
Repeating alarms that are based on a precise trigger time don’t scale well. When possible, use ELAPSED_REALTIME
instead.
Where to Go From Here?
Download the final project using the Download Materials button at the top or bottom of this tutorial.
Congrats! You learned how to use the AlarmManager API, and how to implement exact and inexact alarms — great job!
If you want to learn more about background processing on Android, make sure you check out the Android Background Processing video course that also covers scheduling work with AlarmManager
. Check Scheduling Tasks With Android WorkManager as well to learn more about WorkManager
.
If you have any questions or comments, please join the discussion below! :]