Simple Android AlarmManager Example to Schedule an Event

android-logoAfter getting so many request from users for updating the post on how to use Android alarm to schedule an event. Here is the complete example with source code and description. My idea is to give you a better idea about how AlarmManager works in Android platform. To make the example very simple, in this example we will have an activity which contains a button call ‘Setup Alarm’. Just click the button and wait for 10 seconds. Our Alarm will be raise. We can set any date/time to schedule the
alarm. In this example we have scheduled the alarm time by taking the current time + 10 seconds. So after click the button ‘Set Alarm’, we have to wait for next 10 seconds and we can see a toast message.


Here we will learn about Android AlarmManager example to schedule an event  in a very efficient way.

1. Register AlarmManager BroadcastReceiver.

2. Schedule date/time by using running instance of AlarmManager.

3. Cancel Scheduled Alarm at any time from your code.

Example  Description Details:

  • Create new android project in Eclipse (I am using Indigo) [File >> New >> Android Project] with Project Name: AlarmManagerExample. 
  • Click next and select target SDK 1.6 or higher. You can also just use default values in your eclipse while creating this project.
  • Click next and enter package name – ‘com.techblogon.alarmmanagerexample’
  • Click finish.

1. Layout:
We need to create one simple layout, which will contain one button called ‘Set Alarm’ and a simple textview to show some hints. The text view is optional in this case.


Open your default xml file under /res/layout and replace it with the below XML.


Now coming to our java code. Entire AlarmManager code is here. Have a into below code, then we will discuss line by line.

Code Description:

  • In the above code we have used 3 class variable for complete the AlarmManager task.

  •  To make this example simple, we have created 2 functions called RegisterAlarmBroadcast() and UnregisterAlarmBroadcast(). We will first register the alarm using function RegisterAlarmBroadcast(). In this example we have registered the alarmbroadcast in onCreate() function. Your onReceive() function will be called when your schedule time will be reached.

  • Then when use click on “Set Alarm” button, the function onClickSetAlarm() will be called from java code as above. In this function we will set/schedule the alarm. we have taken current time and add 10 seconds (10000 milliseconds) to schedule the alarm just 10 seconds after the current time.

  • We can also cancel the scheduled alarm by calling the function UnregisterAlarmBroadcast() from any where from your code.

  • AlarmManager needs cleanup after exit from the application. For that we can use the android function unregisterReceiver() from onDestroy() override method.

3. Then build and run the application. Click on ‘Set Alarm” button and wait for 10 seconds. That’s it. Note: For real uses of AlarmManager, it is recomended to use Android background service and separate thread.

downloadDownload the complete sample code from here.

I hope this small tutorial will help you at it’s best.


Post By SmrutiRanjan (57 Posts)

Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.

Website: → Techblogon


One Response to Simple Android AlarmManager Example to Schedule an Event

  1. Mohamed Saleh says:

    i need to enter the time in edit text like that
    alarmManager.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +txtAlert.getText() , pendingIntent );
    but in it an error
    how can i fix it

Leave a Reply

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

eight × 1 =

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="">