Tuesday, 29 May 2012

Multiple Notifications Using AlarmManager

Notifications are used in Two Ways.

1.To start some action, if a particular event happens(Single Notification).
2.To do some particular actions in every time interval(Multiple Notifications).

Here we go through MultipleNotifications, For this we mainly using two classes, Those are
     . AlarmManager
     . NotificationManager

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 setRepeating() method 
of the AlarmManager to repetitive action.

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.

Here is Our classes:


public class NotificationActivity extends Activity {

    AlarmManager am;

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

      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(),
        (10 * 1000), pendingIntent);
      System.out.println("Calling Alaram...");

Here we using  'FLAG_CANCEL_CURRENT'  flag for cancel the current notification and have
 possibilty for next notification. In our example our notification repeated for every 10 seconds.
 For this we can observe in status bar. Notification time will be change, i.e notification repeated
 for every 10 seconds.


public class TimeAlarm extends BroadcastReceiver {

     NotificationManager nm;

     public void onReceive(Context context, Intent intent) {
      nm = (NotificationManager) context
      CharSequence from = "Lokesh";
      CharSequence message = "Notification Test...";
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
        new Intent(), 0);
      Notification notif = new Notification(R.drawable.ic_launcher,
        "Notification Test...", System.currentTimeMillis());
      notif.setLatestEventInfo(context, from, message, contentIntent);
      nm.notify(1, notif);

For this we have to add receiver in manifest.xml

        <receiver android:name=".TimeAlarm" >
                <action android:name="android.intent.action.BOOT_COMPLETED" />

For single notification , we can call the setOneTimeAlarm() method  from the place of setRepeatingAlarm();

Here is the method:
public void setOneTimeAlarm() {
  Intent intent = new Intent(this, TimeAlarm.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);
    System.currentTimeMillis() + (5 * 1000), pendingIntent);



  1. Can AlarmManager be Used for changing the profiles(Silent To General or vice-versa) of phone based on time.If Yes,How?

  2. Will this be compatible by new upcoming androids? desktop notification software

  3. caan i generate the notifiction on set of multiple dates?

  4. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in android programming your post help me a lot.Thanks for your informative article. Android Training in chennai | Android Training institute in chennai

  5. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
    Android Training in chennai with placement | Android Training in velachery