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:

NotificationActivity.java: 

public class NotificationActivity extends Activity {

    AlarmManager am;

     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      setRepeatingAlarm();
     }

      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.

TimeAlarm.java:

public class TimeAlarm extends BroadcastReceiver {

     NotificationManager nm;

     @Override
     public void onReceive(Context context, Intent intent) {
      nm = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
      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

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


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);
  am.set(AlarmManager.RTC_WAKEUP,
    System.currentTimeMillis() + (5 * 1000), pendingIntent);
 }
 






 

21 comments:

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

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

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  6. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    rpa online training

    ReplyDelete
  7. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command

    rpa Training in tambaram

    blueprism Training in tambaram

    automation anywhere training in tambaram

    iot Training in tambaram

    rpa training in sholinganallur

    blue prism training in sholinganallur

    automation anywhere training in sholinganallur

    iot training in sholinganallur

    ReplyDelete
  8. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  9. Really nice experience you have. Thank you for sharing. It will surely be an experience to someone.
    Really nice experience you have. Thank you for sharing. It will surely be an experience to someone.

    ReplyDelete
  10. I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone
    python training in pune
    python online training
    python training in OMR

    ReplyDelete
  11. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    Devops Training in Chennai

    Devops Training in Bangalore

    Devops Training in pune

    ReplyDelete
  12. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    Devops Training in Chennai

    Devops Training in Bangalore

    Devops Training in pune

    ReplyDelete
  13. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    Blueprism training in marathahalli

    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  14. I think you have a long story to share and i am glad after long time finally you cam and shared your experience.
    Blueprism training in marathahalli

    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  15. UiPath Training in Bangalore by myTectra is one the best UiPath Training. myTectra is the market leader in providing Robotic Process Automation on UiPath
    ui path training in bangalore

    ReplyDelete
  16. Pleasant Tips..Thanks for Sharing….We keep up hands on approach at work and in the workplace, keeping our business pragmatic, which recommends we can help you with your tree clearing and pruning in an invaluable and fit way.

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    ReplyDelete