Android Alarm Manager for Scheduling Example

Android provides AlarmManager to access its alarm services. In other words alarm manager is used to schedule your application to be run in future. For example if you want to send a notification at some particular time in future then alarm manager can be configured to send a broadcast message when the alarm goes off. One thing to keep in mind is alarms are cleared off if the device is rebooted. Alarm manager can take use of JobIntentService to process long running tasks. You can download the whole project at the end of the post.

Let’s see how we can use Alarm Manager to register an alarm.

  • Open Android Studio and create new project with empty activity
  • Create broadcast receiver to receive alarm broadcasts.
    • Open File -> New -> Other -> BroadcastReceiver

The above broadcast receiver sends notification on receiving broadcast from the system. This can be used to create background job as broadcast receiver can be killed by the system once onReceive completes and returns.

Add notification id in strings.xml

  • Lets see how to schedule an alarm using alarm manager

After you run this program you will get a notification.

Notification using alarm manager component

Notification

Alarm manager provides different types of alarms. The whole list can be checked here.

 

Alarmmanager
Alarmmanager
Alarmmanager.zip
2.1 MiB
52 Downloads
Details

Leave a Reply