AlarmManager and Notification in Android

In this, I am going to explain AlarmManager and Notification. For Notifying Users in Android, we can use mainly 3 methods as,


The full picture of this can seen here

AlarmManager, we can use in two ways, as

-To start some action, if a particular event happens,
-To do some particular actions in every time interval.

As specified in Android developer site, we will use AlarmManager, when we need to do some actions, even if our application is not running. For normal timing operations use handlers.

For using AlaramManager, first we need to declare the BroadcastReceiver in Manifest file as,

<receiver android:name=".TimeAlarm" />
We need to get the object of the AlarmManager using getSystemService(), then define an intent with parameters as the current context and the BoradcastReceiver class(TimeAlarm). Then define a pendingIntent, we will get the object of it using PendingIntent.getBroadcast(). Now we can call the set() of AlarmManager for doing some action for only once. For repetitive action we have to call setRepeating() of the AlarmManager.

In TimeAlarm, to show to user that our AlarmManager is working we will show a notification in status bar. For that first get the object of NotificationManager using, getSystemService(). Create an object of pendingIntent using PendingIntent.getActivity(). Then create an object of the notification and pass it to the notification manager object.

I created a sample program and the complete source code is,

public class CustomAlarm extends Activity {

 AlarmManager am;

 public void onCreate(Bundle savedInstanceState) {
  am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

 public void setOneTimeAlarm() {
  Intent intent = new Intent(this, TimeAlarm.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
  am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), pendingIntent);

 public void setRepeatingAlarm() {
  Intent intent = new Intent(this, TimeAlarm.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (5 * 1000), pendingIntent);

public class TimeAlarm extends BroadcastReceiver {

 NotificationManager nm;

 public void onReceive(Context context, Intent intent) {
  nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  CharSequence from = "Nithin";
  CharSequence message = "Crazy About Android...";
  PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
  Notification notif = new Notification(R.drawable.icon, "Crazy About Android...", System.currentTimeMillis());
  notif.setLatestEventInfo(context, from, message, contentIntent);
  nm.notify(1, notif);

The screenshots are,


  1. Creative post. I really like your info. Thanks for sharing.

    emergency notification

  2. Great! works! I searched so many sites but didn't find this solution of adding anywhere. Thanks :)

  3. Greeat simple tutorial. :)
    I want to launch my game, on tapping the notification. How can I get the click event on notifiaction??

  4. thanks this helped me

  5. Thanks for your outstanding solution.

  6. nice thanks a lot

  7. Great ! Helped me a lot, thanks !

  8. even 5 years later, it is great. just some parts of the notification is depreaced :)

  9. to avoid deprecated codes with the notification, using notification.builder is a great alternative.


Post a Comment

Popular posts from this blog

Swipe Action and ViewFlipper in Android

Difference between SurfaceView and View in Android