Wake Up with the Alarm

alarmA popular issue in Android-dom is “where is cron?” The reference is to the cron utility in Linux, which runs commands on a pre-determined schedule, such as every hour. While Android does not have cron per se, it does have a similar facility, in the form of AlarmManager. AlarmManager differs from cron in several ways, most importantly:

  1. Whereas cron executes a command, AlarmManager broadcasts an Intent. So, to be woken up by AlarmManager, you need to provide it with an Intent that will cause the right bit of your code to get control when the alarm time rolls around.
  2. The receiver of this Intent must be a BroadcastReceiver registered in your manifest to be filtering on the desired Intent. The cron utility, on the other hand, lets  you run arbitrary commands.
  3. You infrequently have to worry about a PC falling asleep while executing a cron job. On the other hand, this happens a lot with AlarmManager.

You may, if you wish, have AlarmManager wake up the device when an alarm time arrives. In that case, AlarmManager will ensure that your BroadcastReceiver is awake long enough for it to process onReceive()…and nothing more. Since you are not supposed to do a lot of work in the BroadcastReceiver and cannot spawn threads from it, your most likely course of action will be to pass control from the BroadcastReceiver to a Service that is managing a background thread. The work can get done in the thread; the BroadcastReceiver’s thread can return control to Android quickly.

The catch is that the work on the background thread may take too long. AlarmManager has no way of knowing your work is taking too long, and it might allow the device to fall back asleep before your work wraps up.

To combat this, you need to hold a WakeLock.

A WakeLock, obtained from the PowerManager system service, allows you to tell Android to keep the CPU running for a period of time. You want to hold a WakeLock long enough to get your work done and short enough such that you do not use a lot of power. As importantly, you need to make sure you acquire your WakeLock in your BroadcastReceiver, while the CPU is guaranteed by AlarmManager to be still running. The Service is then responsible for releasing the WakeLock when it is done with the background work.

You may also need to hold a WakeLock in other scenarios, particularly if you are playing back media. Android does not keep the device awake and running just because media is being played — you may want to offer your users a configuration option to keep the device awake during playback, and if they select that option, hold an appropriate WakeLock while you are playing the video clip or whatnot.

This is terribly complicated, and over time, we will develop patterns and frameworks that will help with this sort of thing.

To learn more about AlarmManager and WakeLocks:

  • There are many threads on [android-developers] about this, including this one that triggered this blog post
  • You can download some sample code off of this page that demonstrates this technique (scroll down to the Source Code link; the sample code is in SystemServices/Alarm)

About author

AndroidGuys 4639 posts

Founded on November 5, 2007, we've enjoyed bringing you the latest in Android news and rumors. Updated daily, we strive to deliver reviews, opinions, and updates on all things related to Android.

You might also like

News and Rumors

LG Star Could be First Dual Core Android Device, Launch in Korea

LG seems to be back in town with a whole lot of new Androids, and we have to say, impressive ones. Now they are about to launch their first Android phone sporting a dual core processor.

News and Rumors

AG Twitter Weekly Updates for 2009-02-08

Is Google’s ‘Anything Goes’ Policy Hurting the Android Market Reputation? http://tinyurl.com/bq3u2s # SlingMedia’s Android Plans http://tinyurl.com/al6zb2 # AG Twitter Weekly Updates for 2009-02-01 http://tinyurl.com/dmacnk # http://tinyurl.com/byvszb Samsung said to announce

News and Rumors

Early details leak for Samsung GALAXY Tab S

Hey, look! Samsung has yet another new line of products to unveil, or so says SamMobile. This time we’re looking at what’s being billed as the Samsung Galaxy Tab S.


  1. Mike
    March 21, 05:29 Reply

    I enjoyed reading this post about Wake Up with the Alarm | AndroidGuys. Really interesting. I will come back to visit this blog again.

  2. dofollow blogs
    March 21, 14:13 Reply

    Thanks for this great article. I enjoyed reading this post about Wake Up with the Alarm | AndroidGuys. You have great insights about this issue.

  3. low back pain
    March 30, 07:54 Reply

    Thanks for this great article. I enjoyed reading this post about Wake Up with the Alarm | AndroidGuys. You have great insights about this issue.

  4. es7us
    March 31, 03:38 Reply

    so, I notice that a lot of this is over a year old. Are there any recent developments that make this whole process easier, or are we still bound to do it this way?

  5. Eavesdropping
    April 01, 09:29 Reply

    I enjoyed reading this post. You know a lot about Wake Up with the Alarm | AndroidGuys. Thanks for these great insights.

  6. Android Ringtones
    April 26, 04:55 Reply

    That’s a good article about Wake Up with the Alarm | AndroidGuys. Thanks for the info.

  7. Android Ringtones
    April 26, 04:56 Reply

    That’s a good article about Don't Drink Lighter Fluid | Redneck Bar & Grill. Thanks for the info.

  8. Anthoni Gardner
    April 26, 11:22 Reply

    Always enjoy reading Mark's articles. He is a very descriptive person and does not patronize you with long and complex scenarios.

    I've also purchased his PDF books but not had time to go through them all yet. As I am in need of Service / Alarm style event then I think it's time I buried my nose into the Advanced book 😀

  9. Acai Diet
    May 09, 07:07 Reply

    Thanks for this interesting info. That’s a good article about Wake Up with the Alarm | AndroidGuys.

  10. dofollow blogs
    May 13, 05:04 Reply

    I was wondering about Wake Up with the Alarm | AndroidGuys. I was looking for this information for a long time. Thanks for this post!

  11. Dharani Pravantha
    January 02, 06:47 Reply

    i`m searching for, to schedule the task that is after selecting the time from Time Picker Preference(or Dialog picker preference)  photo slide show will be started automatically on that time but i`m unable to write the code. Because, i`m new to android, so please help me

Leave a Reply