Android AlarmManager

Android AlarmManager

Daily we used to set Alarms in our phone to wake up in morning or for some meetings, etc. I used to set lot of alarms for morning but unfortunately i used to wake up late only. ūüėõ ¬†So today we are going to learn about Alarms.¬†Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast. You can schedule your application to run at a specific time in the future. It works whether your phone is running or not.

AlarmManager is having different characteristics mentioned below:

  • AlarmManager runs outside the lifetime of your application. Once an alarm is scheduled, it will invoke even when your application is not running or in sleep mode.
  • An scheduled alarm will execute unless it is stopped explicitly by calling cancel() method, or until device reboots.
  • All scheduled alarms will be stopped when device reboots. This means, you need to re-schedule them explicitly when device boot completes.
  • AlarmManger fires an Intent at given intervals. This can be used along with broadcast receivers to start a service to perform network operations.
  • AlarmManager is different form java Timer and TimerTask.

Android supports two clock types for alarm service; Elapsed real time and real time clock (RTC). Elapsed real time uses the time since device last booted. Real time clock (RTC) uses UTC time for alarm service clock. RTC is most commonly used for setting alarm service in android. The following example, using RTC to schedule alarm.

Prerequisite:

  1. Radio Button.
  2. Notification Manager.
  3. Services.
  4. Broadcast Receiver.

Example

In this tutorial, we will learn how to use AlarmManager that runs after a specific time provided by user.

VIDEO DEMO

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.

2. Open res ‚áí values ‚áí strings.xml and add below string values. These are some strings that we are going to use in our project.

3. Open your activity_main.xml layout and place the below code to it. The layout contains some RadioButton, Edit Text and Button.

4. Now create Broadcast Receiver java class naming AlarmReceiver.java which will invoke by AlarmManger. Here you can do your task like I am running AlarmSound by starting a service and also displaying a Notification.

Alarm Triggered

Alarm Triggered

5. Let us have a look into MainActivity.java file. In this class, we have defined two simple methods triggerAlarmManager(int alarmTriggerTime) and stopAlarmManager().

  • Retrieve PendingIntent : These two lines of use to get the PendingIntent of AlarmReceiver broadcast receiver.
  • Trigger AlarmManager :triggerAlarmManager(int alarmTriggerTime) method use to set the AlarmManager at particular time that we pass in parameter.
Set AlarmManager

Set AlarmManager

  • Stop AlarmManager :stopAlarmManager() method use to stop any running alarm. As I am playing sound and displaying notification as well so i am stopping the sound and cancelling the notification respectively.
Final Source Code
6. As we started sound service in AlarmReceiver.java class, so now create a AlarmSoundService.java class extending with Service. This class will use to play the alarm sound in onCreate() and stop the sound in onDestroy() when user stops the Alarm.

You can download the Alarm Sound mp3 file from this link. Its a good website for Alarm Sounds.

7. Also we are displaying Notification when Alarm starts so lets create new java class naming AlarmNotificationService.java and extend it with IntentService. This class used to show notification.

Alarm Notification

Alarm Notification

8.¬†Also¬†don’t forget to declare BroadCastReceiver, Service and IntentService in Manifest file and as we are showing notification with WAKE_LOCK so use the android.permission.WAKE_LOCK permission. Your AndroidManifest.xml will look like as below.

9. Finally all done, now you can also make apps with AlarmManager.

Thanks. :)

Download Source Code ‚Äď >

About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

COMMENTS (6)
Reply

What Android OS version are you using? because I added a reminder for one of my app, it is working well in Pre Nougat versión but in Nougat version the dayInterval is not working!

    Hi Cristofer,

    I am using Marshmallow OS Version for testing. I have not tested in Nougat OS Devices.

    Thanks.

Reply

How to trigger the alarm for every 5 seconds. I want to hit my server every 5 secs. Can you give me sample for that?

Reply

can you tell me
how to can pick image compressed to gallery
like whats up profile set gallery

Leave a Comment

Comment (required)

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=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)