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