Introduction to AlarmManager

| Page Views: 1451

AlarmManager is a class that allows us create alarms. Alarms allow our apps to schedule specific codes to be executed at certain times in the future.

public class AlarmManager extends Object{}

It's better and more efficient to use AlarmManager class to create alarms for scheduling than using something like a timer.

AlarmManager provides to us the access to system alarm services, so it's not like we are going to invent our scheduling algorithms.

AlarmManager is mostly used together with BroadcastReceivers. Here's how it works:

  • First Alarm goes off or rings.
  • The system broadcasts an intent. This is the intent which had been registered for it.
  • This automatically starts the target application in case it's not already running.
  • If the device sleeps, the alarms that are already registered get retained.
  • If the alarm goes off while the device is sleeping, then the device is woken up. This is optional.
  • If the user turns the device off or reboots it, then the alarms are cleared.

You are guaranteed that the phone will not sleep till you have finished handling your broadcast. Broadcast are handled by the onReceive() method of the android.content.BroadcastReceiver. This is a method you override after deriving this class.

As long as the The onReceive() method is still executing, the AlarmManager will hold a CPU wake lock. So the device won't sleep.

Then the AlarmManager releases the wake lock when the onReceive() finishes executing and returns.

But sometimes just as the onReceive() method finishes, it's possible that the phone can sleep immediately. Because the device has slept quickly, if you had requested a service using the Context.startService() it won't be started. This is because the device has slept before it's called. However, the initial wake lock is no longer in place. It had been released the moment the onReceive() had returned. So what's the solution? Well you implement a separate wake lock on your BroadcastReceiver and Service. This wake lock will ensure the device runs until the service becomes available.

So when should you use alarm manager and when should you not? Well use alarm manager for scheduling operations. Don't use it for timing and ticking operations. And don't use timers for scheduling operations. Scheduled code using alarmmanager do not require teh application to be running all the time. If you used a timer then it would have to run throughout. This wastes memory and processing time.

The Android Operating System shifts alarms so us minimize wakeups and battery use. This is starting from Android API 19(KitKat). This the alarms may not be strictly exact. If you need to be strictly exact, then you can use the setExact() method.

AlarmManager is not directly instantiated. Instead you use the static getSystemService() of the Context class. You pass the Context.ALARM_SERVICE flag.


Top Android Alarm Examples

Let's look at some examples.

Android Engineers added AlarmManager class in API level 1, so it's been around since the beginning of android. This class allows for scheduling of operations to be done sometime in the future. With AlarmManager, you can set some code that will be executed in the future.

This is cool given that it's not necessary for your app to be running for that code to be run. Of course your app will be started, but only at the registered time. Alarm Maanager belongs to package and inherits from java.lang.Object.

AlarmManager has existed since the beginning of Android. It enables us schedule tasks to be done sometime in the future.

With AlarmManager, you can set some code that will be executed in the future.

Alarm Maanager belongs to package and inherits from java.lang.Object. Even while the device is asleep, alarms are retained as long as they were registered. In this example we will see how to work with a Recurring/repeating alarms.

Best Regards, Oclemy.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post