Alarm Systems
Technology
Security
Home Safety
Alarm Management

Alarm Manager Example

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

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

2. Obtaining AlarmManager

You can obtain an instance of AlarmManager by calling getSystemService() in an Activity or Service.

java
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

3. Creating a PendingIntent

The AlarmManager works with all types of PendingIntent. We commonly use getBroadcast() for it to send a broadcast.

java
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

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.

java
1if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
2    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);
3} else {
4    alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);
5}

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.

java
1Calendar calendar = Calendar.getInstance();
2calendar.set(Calendar.HOUR_OF_DAY, 8);
3calendar.set(Calendar.MINUTE, 0);
4calendar.set(Calendar.SECOND, 0);
5long startTime = calendar.getTimeInMillis();

If the calculated startTime is in the past (i.e., it's already later than 8 AM today), add one day.

java
if (System.currentTimeMillis() > startTime) {
    startTime += AlarmManager.INTERVAL_DAY;
}

Then, set the repeating alarm.

java
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startTime, AlarmManager.INTERVAL_DAY, pendingIntent);

Summary Table

MethodType of AlarmWake DeviceRepeating CapabilityUse Case
set()Based on method parameterOptionalNoSingle event, precise timing
setRepeating()Based on method parameterOptionalYesRecurring events, inexact timing
setExact()Based on method parameterOptionalNoSingle event, precise timing
setExactAndAllowWhileIdle()RTC_WAKEUPYesNoEnsure 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.


Course illustration
Course illustration

All Rights Reserved.