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

Layouts

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

alarm.xml

main.xml

Wrap-up

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.

  • dmarg

    Thank you very much for this code! It was exactly what I was looking for.

  • Sam

    Succinct, thanks

  • vizviz

    if instead of 5 second i want to set alarm in a custom date for example 2012/6/20 What can I do?

    • http://iamnerdwin.com/ Michael

      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.

      • Mehrdxd

        thanks man!

  • ankita

    what are the permission should be taken for run this coding

    • http://iamnerdwin.com/ Michael

      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.

  • Javed Siddiqui

    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.

  • Jinesh D

    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. :)

    • http://iamnerdwin.com/ Michael

      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

        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.

  • its_dark

    Great Tutorial !!

  • Aurang Zeb Pti

    Thanks. Nice and simple.