By Joyce Echessa

Scheduling Background Tasks in Android

By Joyce Echessa

Periodically your Android application might need to perform a task, such as checking a server for updates. The AlarmManager class can be used to schedule and execute operations that need to occur even if the application isn’t running.

The AlarmManager class enables the scheduling of repeated alarms that will run at set points in the future. The AlarmManager is given a PendingIntent to fire whenever an alarm is scheduled. When an alarm is triggered, the registered Intent is broadcast by the Android system, starting the target application if it’s not already running.

Alarm Type and Precision

There are two major alarm types, Elapsed Realtime and Real Time Clock. The first corresponds to the time since system boot and the second to UTC time. The different alarm types can be set to either wake up the device’s CPU when in sleep mode, or to fire when the device is next awake. Below is a list of the different alarm type variations available.

  • ELAPSED_REALTIME – Fires the pending intent after the specified length of time since device boot. If the device is asleep, it fires when the device is next awake.

  • ELAPSED_REALTIME_WAKEUP – Fires the pending intent after the specified length of time since device boot. It wakes up the device if it is asleep.

  • RTC – Fires the pending intent at a specified time. If the device is asleep, it will not be delivered until the next time the device wakes up.

  • RTC_WAKEUP – Fires the pending intent at a specified time, waking up the device if asleep.

Deciding on the alarm to use depends on the requirements of the task at hand. Time elapsed is suitable for setting alarms that should fire based on the passage of time, for instance regularly checking a server for changes. If you want to run a task dependent on the current locale, then the real time clock will be more suitable.

Note that elapsed time is usually the better choice. If the user changes their time settings or moves to a new locale, the change might cause some unexpected behaviour in the app. If you set a specific time for an app to sync with a server, the server could be overwhelmed when all instances of the app hit at the same time.


Setting up a recurring task

We’ll create a simple application to show the scheduling of tasks. The task we’ll schedule will be the displaying of a message using Toast (A toast provides simple feedback about an operation in a small popup). Code for this project can be found at this Git Repo.

Create a new Android project. For the view, create two buttons that will start and stop the periodic operations.

In res/layout/activity_main.xml

<LinearLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:text="Start Alarm"
        android:onClick="startAlarm" />

        android:text="Cancel Alarm"
        android:onClick="cancelAlarm" />


To receive intents, we’ll set up a broadcast receiver to perform an operation when the alarm is fired. Create a class that inherits from BroadcastReceiver. In the onReceive method, which is called when the BroadcastReceiver is receiving an Intent broadcast, we will set up the code that runs our task.


package com.example.alarmexample;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

    public void onReceive(Context arg0, Intent arg1) {
        // For our recurring task, we'll just display a message
        Toast.makeText(arg0, "I'm running", Toast.LENGTH_SHORT).show();



We then need to register the BroadcastReceiver in the manifest file. Declare the AlarmReceiver in the manifest file.

    <receiver android:name=".AlarmReceiver"></receiver>

To start the alarm we’ll set up the method startAlarm declared in res/layout/activity_main.xml above as the method to run when the Start Alarm button is clicked.

In declare a PendingIntent and an AlarmManager variable. The PendingIntent will be used to set and cancel the alarms.

In include the following instance variables.

private PendingIntent pendingIntent;
private AlarmManager manager;

In onCreate() we create an Intent that references our broadcast receiver class and uses it in our PendingIntent.

protected void onCreate(Bundle savedInstanceState) {

    // Retrieve a PendingIntent that will perform a broadcast
    Intent alarmIntent = new Intent(this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);

We then include the method that will set up the recurring alarms. Once set, the alarm will fire every 10 seconds.

public void startAlarm(View view) {
    manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    int interval = 10000;

    manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
    Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();

Run the application and by clicking the “Start Alarm” button an “Alarm Set” message will appear, followed by the message “I’m running” every 10 seconds.

We used the setRepeating() method to set up a recurring alarm, but setInexactRepeating() could also be used. With setInexactRepeating() repeating alarms from multiple applications will be syncronized and fired at the same time. This will reduce the number of times the device is woken up, saving battery power. As of Android 4.4, all repeating alarms are inexact.

The setRepeating() method takes four arguments:

  1. type – alarm type, specified by the units of time to use and whether or not it should occur when the device is in sleep mode. Can be ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC, or RTC_WAKEUP.

  2. triggerAtMillis – time in milliseconds that the alarm should first fire.

  3. intervalMillis – interval in milliseconds between subsequent repeats of the alarm.

  4. operation – Action to perform when the alarm fires

Next, we’ll set up the cancelAlarm() method to stop the alarms. Include the method below in

public void cancelAlarm(View view) {
    if (manager != null) {
        Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();

Before running the application again, Force Stop or uninstall the app in your device or simulator. If you don’t, the previous alarm schedule will still be running. Run the application and the scheduled alarm can be stopped.

The above is a simple example showing how to use the AlarmManager to set up and cancel alarms. I use the same AlarmManager object to start and stop the alarm. This of course will not persist when the app is exited, so for a real app instead of only canceling the alarm when manager is not null (which it will be if the app was exited, no matter if the alarm is running or not), you should cancel the alarm with an equivalent PendingIntent to the one used setting it. For example you could do this instead.

Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();


We have looked at how the AlarmManager can be used to schedule periodic tasks. The AlarmManager is best used for tasks that need to run even when the application isn’t open. For normal timing operations that need to run during application use (ticks, timeouts, etc), it is more efficient to use the postDelayed() and postAtTime() methods of a Handler. The AlarmManager requires too much overhead to justify it being used this way.

  • Great article Joyce. One more thing i would suggest is when you are registering a for a broadcast receiver, its always a good approach to specify a complete location of your receiver java file with the package name i.e. com.mypackage.helpers.MyReceiver

  • Chris Ward

    Thanks for your comments Mutinda, more from Joyce very soon!

  • Thanks for that. Yeah, you are right, it is a good practice to use the full qualified names, especially as projects get bigger.

  • Vikram Tiwari

    Hello Joyce,
    A small question: In a hypothetical scenario where I have multiple applications installed on an android device and these individual applications have created their respective RTC_WAKEUP alarms. Since these applications are not running when alarms are fired, I was wondering if Android gives a way of looking up (for example under Settings..) what all alarms are pending for what applications?

    How do you typically troubleshoot such applications?

    Thanks in advance,

    • Hi Vikram,
      Do you mean being able to check the set alarms (and/or modify them) as an end user?

      As an end user, you are restricted by what the app developer allows you to control and see. Lets say, there is an app that has to sync with a server N number of times a day or after N number of minutes. This is usually not information made available to users, but some applications offer the user an option of cancelling automatic updates, thus stopping the alarm from running. But I have never seen an app that offers this information to users.

      So, to answer your question, if there is a way of checking all the set alarms of all applications installed on your device. No, there isn’t (none that I am aware of at least).

      I hope that answers your question. If it doesn’t let me know.

  • Prabhu

    Hi Joyce,

    I am trying to create the reminder application that remind the user to do their regular activities. For that I use the alarm manager to set repeating alarm. (inexact repeat, RTC time), the hour and minutes for the alarm are user choice. I get the time from the end user and set it into app by using Calender instance. I am able to receive the notification, but the problem is time. If I set the alarm time 12:23 PM, I get the notification from the app on 12:30 PM or above. The application took more time to fire the alarm.

    I am not able to find actually what is going on here. Can you please help me to find out the timing problem.. (i assume it is related to the processor).


  • Gui HGO

    Thanks !!!!

  • flutin

    great demo!
    only thing i would add is maybe screenshots.

  • Krish

    Hi Joyce,
    I am new to Android programming and application development. Now that seeing your code I understood certain concepts. Awesome explanation. Thanks for that. One query I’ve got for ya is., if i set an alarm and close the application. Now when I say closing application, I mean the usual way we clear all the background running applications. Will the alarm still fire? Cuz I tried a lot but no luck. It’s not firing the alarm. Could you pls help me solve this?

  • Guest

    Hi Joyce,
    My app drains battery hard, it has alarm(RTC) fires every minute and alarm(RTC_WAKEUP) fires every ten seconds. 2nd alarm start Service with data in intent(cause I’m not able to make bindService from receiver). I know that 10-seconds alarm is quite bad.
    I have a question: what exactly can drain my battery, 2nd alarm or service?
    Thanks a lot!

  • Shivanandana

    I don’t know who you are but if I find you…… I’ll THANK YOU…!!! Really… great article.. I was searching for something like this from a long time…. Thank you soooooo much…!!!

    Just one question. Can I have a count as to how many time my Alarm was scheduled?? Say I want to schedule the Alarm only 20 times a month. How can I achieve this??

    Thanks in advance…!!!

  • ElimiNathan

    Using CPU power drains the battery, waking up a device every 10 seconds is keeping the CPU cycle alive constantly, hence battery drain. What logic does your Service run ? That could be a contributing factor on top of the 10 second alarm

  • lol I pictured Liam Neeson (Taken) saying that first part.

    I haven’t tried this, so I’m not sure if it’ll work, but this is how I would go about it:

    I would persist the alarm count in memory. So it would start at 0 and every time the alarm fires, I would increment the number. If it gets to 20, cancel the alarm.
    I would do this in the onReceive() method in the BroadcastReceiver since this is called when the alarm fires.

  • Shashank Gudipati

    Hi Joyce,
    I have used alarm manager to start a service that builds a notification to user. However when I force close my app, I am getting ‘Unfortunately your app stopped working’. My main activity (onCreate) has time and date picker that are used to set Alarmmanager, which in turn is used to invoke a Alarmreceiver thru a pending intent. I am not sure why app is not able to close cleanly ? Is it because alarmmanager is still running in background or NotificationService might be causing some problem ?

    Thanks in advance!!

    • Please post your logcat output and others might be able to help you :)

  • Thank you! I’ve been experimenting with Handlers, Executors, all to no avail. This was what I needed!

  • Vatsalya Chauhan

    Hey, I need some help.

    I want to create some popup that will take input from the user inside onReceive method. like ( . But i am unable to do that, i have tried it many times (by creating new intent). Please show me some path.

  • Ankur Kashyap

    Hey nice article Joyce.Helped me a lot.
    But i am having a problem. Above code doesn’t work after rebooting the system. Should i add intent-filter “receive_boot_completed” in my receiver? And if i add it, how can i start the same AlarmManager intent? Please provide me little code snippet for my problem. Thank you.


    Thanx a lot, it’s very useful. ;)

  • Miguel Ángel

    Thanks! Quite clearly explained. It helped me a lot. :)

  • ranjithstar256

    hi joyce thanks for the post and clear explanation :) Can i have the working example code i mean to download it as a zip file? thanks :)

  • sourabh

    thanks it help me alot

  • Amir

    Good Article

  • serious

    Nice job, saved my day!

  • idroid_S

    Hello, thanks for the great article!

    I have one question though, for some reason, my task only fires 1 minute apart, even though I put the interval as 10000 (Which should be 10 seconds), going less also had the same effect, but if I put an interval of 2 minutes, it works, is there some sort of minimum time?

    What do you think is the problem?

  • fillobotto

    Exactly what I was looking for

  • ahmed hamed

    Finally a great explanation, thanks for the last part

    • Chris Ward

      Thanks @disqus_DiFhOwFxTE:disqus

  • cacus

    Excelent explanation, thank you !

  • Nicky N

    Thank you very much. Asante sana

  • Ubaid

    Thanks, this article was save my live
    but i have problem, when I put the cancel button in other activity i doesn’t work.
    what should I do about this problem?

  • Soumyajit Chowdhury

    It doesn’t works in android 6.0. Can u please give a relevant solution for that. Whenever I set the alarm, and kill the application, the alarm also gets killed in Marshmallow and doesn’t work in the background. :(

Get the latest in Mobile, once a week, for free.