Dry Goods
Briefly extract important knowledge points about broadcast (broadcast.
Send broadcast in different ways
Sendbrodcast
Generally, the onreceive method is executed for all broadcastreceive methods that meet the conditions, but the execution sequence is not guaranteed.
Sendoraderredbroadcast
Ordered broadcast: The intent (intent) sent executes the onreceive (accept) method based on the priority set by the intentfilter registered by broadcastreceive.
Sendstickybroadcast
Sticky broadcast, the intent it sends will always exist after it is sent, and the intent will be directly returned to the newly registered receiver when you call registerreceiver to register a matched receive.
Static dynamic Broadcast
Static broadcast:
In the inventory file (androidmanifest. XML), use the <receiver ER> label to declare registration, and use the <intent-filter> label in the tag to set the filter.
Dynamic Broadcast :
Register and useRegisterreceiverMethod To destroyUnregisterreceiverIn addition, when the context corresponding to broadcastreceiver is dynamically registered for destruction, broadcastreceiver will alsoAutomatically cancel registration.
Real Goods
Yes, In order to correspond to the above dry goods, but it's a bit nondescribable, but it's okay, it's a big eye.
The following is a simple example of static broadcast and dynamic broadcast. I will extract some important information.CodeFor details, refer toSource codeThe source code of a large part is still in IDE (integrated development tools are good). If you can watch my blog while watching eclipse, the effect is better.
Static Broadcast
1. List list (added after activity ):
1:<! -- Register a custom static broadcast Receiver -->
2:<Cycler Android: Name ="Com. example. broadcastexample. staticreceiver">
3:<Intent-filter>
4:<Action Android: Name ="Com. BN. My. staticreceiver"> </Action>
5:</Intent-filter>
6:</Cycler>
2. Publish broadcast:
1:// Button variable
2:PrivateButton sendstaticbutton;
3:PrivateButton senddynamicbutton;
4:// Static variables
5:Private StaticFinal string staticaction ="Com. BN. My. staticreceiver";
6:Private StaticFinal string dynamication ="Com. BN. My. dynamicreceiver";
7:
8:@ Override
9:Protected VoidOncreate (bundle savedinstancestate ){
10:Super. oncreate (savedinstancestate );
11:Setcontentview (R. layout. activity_main );
12:// Get the button and register the event
13:Sendstaticbutton = (button) findviewbyid (R. Id. btn_sendstaticbroadcast );
14:Sendstaticbutton. setonclicklistener (NewDiyonclicklinstener ());
15:Senddynamicbutton = (button) findviewbyid (R. Id. btn_senddynamicbroadcast );
16:Senddynamicbutton. setonclicklistener (NewDiyonclicklinstener ());
17:}
Internal class diyonclicklinstener
1:ClassDiyonclicklinstener implements onclicklistener {
2:
3:@ Override
4:Public VoidOnclick (view v ){
5:// Todo auto-generated method stub
6:Intent intent =NewIntent ();
7:If(V. GETID () = R. Id. btn_sendstaticbroadcast ){
8:Intent. setaction (staticaction );
9:Intent. putextra ("MSG","Static registration broadcast accepted! ");
10:}
11:Else If(V. GETID () = R. Id. btn_senddynamicbroadcast)
12:{
13:Intent. setaction (dynamication );
14:Intent. putextra ("MSG","Dynamic broadcast accepted! ");
15:}
16:// Publish Broadcast
17:Sendbroadcast (intent );
18:}
19:
20:}
3. Accept Broadcast
In fact, the list tells the system that my accept Class is staticreceiver. This class must inherit broadcastreceiver and we can create a new class.
1:Public ClassStaticreceiver extends broadcastreceiver {
2:
3:@ Override
4:Public VoidOnreceive (context, intent ){
5:// Todo auto-generated method stub
6:String MSG = intent. getstringextra ("MSG");
7:Toast. maketext (context, MSG, Toast. length_short). Show ();
8:}
9:
10:}
Dynamic Broadcast
1. Publish broadcast. The code here is found at the publish broadcast of static broadcast.
2. Register dynamic broadcast in the onstart method, enter it in the configuration file, and specify the event for receiving the broadcast.
Note,ProgramThe running sequence of is oncreate ()-> onstart ().
1:PrivateBroadcastreceiver dynamcireceiver =/**
2:* @ Author haichao
3:* Dynamically added accept events
4:*/
5:NewBroadcastreceiver (){
6:
7:@ Override
8:Public VoidOnreceive (context, intent ){
9:// Todo auto-generated method stub
10:If(Intent. getaction (). Equals (dynamication )){// Action Detection
11:String MSG = intent. getstringextra ("MSG");// Get intentn content
12:Toast. maketext (context, MSG, Toast. length_short). Show ();// Display
13:}
14:}
15:
16:};
17:/* (Non-javadoc)
18:* @ See Android. App. Activity # onstart ()
19:* Add broadcast dynamically
20:*/
21:@ Override
22:Protected VoidOnstart (){
23:// Todo auto-generated method stub
24:Super. onstart ();
25:// Add intent Filter
26:Intentfilter dynamic_filter =NewIntentfilter ();
27:Dynamic_filter.addaction (dynamication );
28:Registerreceiver (dynamcireceiver, dynamic_filter );
29:}
Do you think the variable dynamicireceiver is similar to the staticreceiver that inherits broadcastreceiver? Yes, the same function is provided here, but it is dynamic here.