Android – Creating an Alarm with AlarmManager

There aren’t a lot of good write-ups on how to use the AlarmManager, so here is an example that launches an Activity using the AlarmManager.

What’s the AlarmManager used for?

The AlarmManager is used to schedule events or services at either a set time or a set interval. It’s Android’s “version” of the cron. In this case, we’re going to set an alarm for five seconds after the app is launched.

What we’re going to build

In order to use build this simple app, we’re going to create only two classes. One will be for the Main Activity, and one will be for the Activity we want to launch with the Alarm. We will also have two simple layouts for each of the activities.

The Main Activity

In this Activity, we are setting the layout, setting an offset for five seconds from now, creating the PendingIntent and adding it to the AlarmManager. The PendingIntent.FLAG_CANCEL_CURRENT tells the AlarmManager that any other pendingIntent’s with the same id (in this case 12345) should be canceled and replaced with this one. If you want to have more than one alarm, you’ll need to change the 12345 in line 16 to make each alarm unique.

The AlarmReceiverActivity

When this Activity starts, we are ensuring it’s full-screen, playing a sound, and making sure there’s a way to stop the sound. Not too difficult…

Don’t forget to add the Activity to your AndroidManifest.xml


In case you’re new to Android development, these are put into your res/layout folder.




Run that and see how it goes! Feel free to leave any comments below!

To check out the source code for this project, you can find it on Github.

14 replies
    • Michael
      Michael says:

      It doesn’t matter all too much how you build the date, as long as you can get the time in milliseconds.

      If you wanted to set it to a specific date, you could use a SimpleDateFormat object to parse a String to turn it into a Date object. Then use the getTime() to the get the time in milliseconds. There’s several other ways this could be approached, but hopefully that helps you know where to start looking.

    • Michael
      Michael says:

      Feel free to take a look at the sample project on GitHub (link at the end of the post). You’ll see that there are no additional permissions that are required to use an AlarmManager.

  1. Javed Siddiqui
    Javed Siddiqui says:

    I am beginner in Android. Thanks for ur post. It helps me a lot. But I want to set an alarm using picker. How would I do that. Thanks for ur support.

  2. Jinesh D
    Jinesh D says:

    Hi, When I don’t call the playSound(), The alarm goes off and then when I click the Stop button, the system throws an error “Unfortunately, alarmManager has stopped” ?? Any idea why is that !!?

    Also, after this happens the Main activity becomes active and the alarm again goes off in 5 seconds..and the error repeats and the this entire process repeats.!!

    But when I call the PlaySound() and the alarm goes off and clicking the stop button brings it back to the Main Activity..but this time the alarm doesnt go off again eventhough the Main activity is active.. Again any idea why ??

    Thanks for the wonderful post. I look forward to hear from you. :)

    • Michael
      Michael says:

      Hi Jinesh! I’d be happy to help out. But, it’d be hard to answer what’s going on without seeing what you’ve got. Are you trying to run the sample code or using your own project? If your own project, I can take a look at it if you put post it somewhere.

      • Jinesh D
        Jinesh D says:

        Hi Michael,
        Thanks for the quick reply. I used the above code as it is. But was just playing with it to try to understand it better. So I just commented out the playSound() that is made in the AlarmReceiverActivity and I get the above things that I mentioned in my previous comment.

  3. anees
    anees says:


    Great Post!

    Can you clarify something for me?

    Does the application needs to be running to receive the event? I mean, will it work if the application has been closed from the android window manager(swipe to close feature)?


Trackbacks & Pingbacks

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">