Alarm Manager Example
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
The Android Alarm Manager provides a way to schedule your application to run at a specific time or interval, even if your app is not currently running. This makes it particularly useful for tasks requiring regular execution or for setting a future point in time in your app, independent of its current state.
Overview
The Alarm Manager operates outside of your application lifecycle, managed by the system, which invokes your app at a scheduled time via an intent. This ensures that the wakeup of your device from sleep for the alarm's intention leads to minimal power consumption, aligning with Google’s emphasis on extending battery life.
Implementation Steps
1. Set Up Permission
First, you need to add the SET_ALARM permission in your AndroidManifest.xml.
2. Obtaining AlarmManager
You can obtain an instance of AlarmManager by calling getSystemService() in an Activity or Service.
3. Creating a PendingIntent
The AlarmManager works with all types of PendingIntent. We commonly use getBroadcast() for it to send a broadcast.
4. Setting the Alarm
Now, you can set the alarm using one of the methods like set(), setRepeating(), or setExact(). Use setExact() for exact timing of execution.
Types of Alarms
Alarm Manager supports various types including:
RTC_WAKEUP: Wakes up the device to fire the pending intent at the specified time.RTC: Does not wake the device up; the event occurs only if the device is already awake.ELAPSED_REALTIME: Uses time since system boot as a reference, but does not wake up the device.ELAPSED_REALTIME_WAKEUP: Uses time since system boot as a reference and wakes up the device.
Example Use-Case: Setting a Daily Notification
Imagine you want to set up a daily notification at 8 AM for the user to check new updates in your app.
First, calculate the time at which the alarm should first go off.
If the calculated startTime is in the past (i.e., it's already later than 8 AM today), add one day.
Then, set the repeating alarm.
Summary Table
| Method | Type of Alarm | Wake Device | Repeating Capability | Use Case |
set() | Based on method parameter | Optional | No | Single event, precise timing |
setRepeating() | Based on method parameter | Optional | Yes | Recurring events, inexact timing |
setExact() | Based on method parameter | Optional | No | Single event, precise timing |
setExactAndAllowWhileIdle() | RTC_WAKEUP | Yes | No | Ensure execution in low-power modes |
Final Considerations
When using Alarm Manager, consider the impact on battery life and try to minimize it by using inexact repeating alarms whenever possible. Furthermore, remember that scheduled alarms are cleared if the device is rebooted. Thus, it may be necessary to reschedule them upon boot completed by using a broadcast receiver for android.intent.action.BOOT_COMPLETED.
In conclusion, the Android Alarm Manager is a powerful scheduling tool that facilitates time-based operations outside of an application lifecycle, making it immensely beneficial for tasks like notifications, reminders, or any operations that need to be performed at a specific time every day, week, or even month.
Related reading
- Algorithm Complexity Security MD5 or SHA1?
- Allowing Untrusted SSL Certificates with HttpClient
- Am I trying to connect to a TLS-enabled daemon without TLS?
- Amazon Cognito A client attempted to write unauthorized attribute
- Amazon DynamoDB InvalidSignatureException
- Amazon DynamoDB InvalidSignatureException
- Amazon EC2 instances multiple IAM roles
- Amazon EC2 Permission denied publickey

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.