About the registered broadcast of Pendingintent.getbroadcase in Android

Source: Internet
Author: User

Using statements

[Java] View plaincopyprint? Pendingintent intent= Pendingintent.getbroadcast (context context, int requestcode, Intent intent, int flags)

Pendingintent intent= Pendingintent.getbroadcast (context context, int requestcode, Intent intent, int flags)

Get pendingintent, browse a variety of articles, most of which said this method, but basically stop here, but there is the most important not to talk about how to distinguish between multiple registered pendingintent, After looking at the Javadoc of Pendingintent.getbroadcast, the fourth parameter flags, the first thought that flags were to identify each pendingintent, then set a global variable in the test.

[Java] view plaincopyprint?public static int currentintent=0;

public static int currentintent=0;
Then use currentintent++ as the fourth parameter passed in, test, registered two listening, wait for the arrival of time, bingo, incredibly can, visual already can. But the question goes on, how do I pass the parameters? The positive solution is to set in the third parameter

[Java] View Plaincopyprint?intent.setextra (String key,string value); Set parameters for passing

Intent.setextra (String key,string value); Set parameters for passing
And then, in the receiver I realized, intent intent with the parameters passed in.

[Java] View Plaincopyprint?intent.getintegerextra (String key);

Intent.getintegerextra (String key);
Can be obtained parameters, can be really realized at the time of the discovery, in receiver always take the parameters, and then after a search, found to the pendingintent.getbroadcast of the fourth parameter set in Pendingintent.flag_ Update_current, set after the test, sure enough, but this problem came out again, and how to distinguish between the registration of intent it? Look again Getbroadcast Javadoc, a few parameters do not explain how to distinguish the pendingintent to register, but see the second parameter description is magic, is this parameter is currently reserved state, still unused, helpless, continue to search various statements, Only to find that it is possible to distinguish with requestcode (but why Javadoc to say that the parameter is not used?) An estimate of the method used to differentiate pendingintent is that any one of these parameters can be distinguished by the following code:

Setting up Monitoring

[Java] View plaincopyprint? Intent setalertintent=new Intent (this,alertreceiver.class);
Setalertintent.putextra ("Try", "I ' m just have a try");
Pendingintent Pendingintent=pendingintent.getbroadcast (This, alarmcount++, Setalertintent,pendingintent.flag_ Update_current);

Alarmmanager alarmmanager= (Alarmmanager) Getsystemservice (Alarm_service);
Alarmmanager.set (Alarmmanager.rtc_wakeup, Calendar.gettimeinmillis (), pendingintent);

Intent setalertintent=new Intent (this,alertreceiver.class);    Setalertintent.putextra ("Try", "I ' m just have a try");    Pendingintent Pendingintent=pendingintent.getbroadcast (This, alarmcount++, Setalertintent,pendingintent.flag_ update_current);    Alarmmanager alarmmanager= (Alarmmanager) Getsystemservice (alarm_service);    Alarmmanager.set (Alarmmanager.rtc_wakeup, Calendar.gettimeinmillis (), pendingintent);

Get the data passed in receiver:

[Java] View plaincopyprint?public void OnReceive (context context, Intent Intent) {

TODO auto-generated Method Stub   Bundle bundle= Intent.getextras ();  if (bundle==null) {      Toast.maketext (context, "nothing", Toast.length_long). Show ();  else{      set<string> set=bundle.keyset ();      for (String item:set) {          System.out.println (item);          System.out.println (".......");      }      Toast.maketext (Context,bundle.getcharsequence ("Try"), Toast.length_long). Show ();  

}

public void OnReceive (context context, Intent Intent) {    //TODO auto-generated method stub    Bundle bundle= INTENT.G Etextras ();    if (bundle==null) {        Toast.maketext (context, "nothing", Toast.length_long). Show ();    else{        set<string> set=bundle.keyset ();        for (String item:set) {            System.out.println (item);            System.out.println (".......");        }        Toast.maketext (Context,bundle.getcharsequence ("Try"), Toast.length_long). Show ();    }}

About the registered broadcast of Pendingintent.getbroadcase in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.