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);
 }
 






 

56 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. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. Wonderful information, thanks a lot for sharing kind of information. Your website gives the best and the most interesting information. Thanks a ton once again
    Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete
  13. Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  14. indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
    SOFTWARE TRAINING IN CHENNAI
    POWERBI TRAINING IN CHENNAI
    CCNA TRAINING IN CHENNAI
    ANDROID TRAINING IN CHENNAI

    ReplyDelete
  15. For Hadoop Training in Bangalore Visit : HadoopTraining in Bangalore

    ReplyDelete
  16. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful .Manual Testing Training in Bangalore

    ReplyDelete
  17. I blog frequently and I genuinely thank you for your information. This great article has really peaked my interest. I will book mark your blog and keep checking for new details about once per week. subject I opted in for your Feed as well.

    ReplyDelete
  18. Are you unable to execute the process of Binance two-factor authentication in the Binance exchange? If you come across such issues on a regular basis and now, you are looking for solutions to handle all troubles, good luck
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  19. Thanks for sharing information awesome blog post Online Education Quiz website For Exam Follow this website Gk in Hindi

    ReplyDelete
  20. 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 DevOps your post help me a lot.Thanks for your informative article.
    DevOps Training in Chennai

    DevOps Course in Chennai


    ReplyDelete
  21. Technologies are with you to make your dream into reality. Infycle Technologies is one of the best Big Data Training Institute in Chennai, which offers various programs along with Big Data such as Oracle, Java, AWS, Hadoop, etc., in complete hands-on practical training with trainers, those are specialists in the field. In addition to the training, the mock interviews will be arranged for the candidates, so that they can face the interviews with the best knowledge. Of all that, 100% placement assurance will be given here. To have the words above in the real world, call 7502633633 to Infycle Technologies and grab a free demo to know more.Big Data Training in Chennai

    ReplyDelete
  22. Amazing opportunities came with amazing time and here is our institution offering you CS executive classes and a free of cost CSEET classes. So what are you waiting for contact us or visit our website at https://uniqueacademyforcommerce.com/

    ReplyDelete
  23. Amazing opportunities came with amazing time and here is our institution offering you CS executive classes and a free of cost CSEET classes. So what are you waiting for contact us or visit our website
    cs executive
    freecseetvideolectures/

    ReplyDelete
  24. Infycle Technologies, the best software training institute in Chennai offers the No.1 Python Certification in Chennai for tech professionals. Apart from the Python Course, other courses such as Oracle, Java, Hadoop, Selenium, Android, and iOS Development, Big Data will also be trained with 100% hands-on training. After the completion of training, the students will be sent for placement interviews in the core MNC's. Dial 7502633633 to get more info and a free demo.

    ReplyDelete
  25. The CHF USD Converter Is Updated Every 15 Minutes. You Can Set It To Alert You Whenever The Rate Changes.

    ReplyDelete
  26. AximTrade Is A Forex And Cfd Broker. It Offers Trading In Currency Pairs, Commodities, Indices, And Shares. It Also Provides A Range Of Tools, And 24/7 Customer Service. Sign Up For Aximtrade Login Account Today!

    ReplyDelete
  27. Oracle is one of the easiest and most valuable jobs!! From Infycle’s Oracle training in Chennai, we are able to see more and more training places like this but only INFYCLE holds the name No.1 Software training and placement in Chennai because INFYCLE is built by trust. For more details contact 7502633633. Oracle Course in Chennai | Infycle Technologies

    ReplyDelete
  28. Know the Latest Smartphone Price with MobileErBazaar.com totaly free

    Some Peopole are want to know Vivo V23e price of bangladesh it's easy to know

    ReplyDelete
  29. It is amazing and wonderful to visit your site. Thanks for sharing this information
    jewellery shop management software
    Jewellery Shop Management Software

    ReplyDelete
  30. Appear tough myself skill effort economy cut early.health

    ReplyDelete