In the last blog post briefly introduced the relevant knowledge points of Broadcastreceiver, this chapter is a dynamic registration in the code, write-off broadcastreceiver chestnut.
1, first create a myreceiver and inherit the Broadcastreceiver, since to register the receiver dynamically, there is no need to configure it in Androidmanifest.xml:
1 Public classMyreceiverextendsBroadcastreceiver {2 Public Static FinalString ACTION = "Com.codingblock.receive.intent.action.MyReceiver";3 4 PublicMyreceiver () {5 6 }7 8 @Override9 Public voidOnReceive (Context context, Intent Intent) {TenSystem.out.println ("The Received message is:" + Intent.getstringextra ("Data")); One } A}
Code Analysis: A static variable action is defined in the code above to facilitate the program to specify the receiver. In addition, from the second parameter of the OnReceive () method intent can get the data passed.
2, layout file as follows, very simple, only three buttons: Send Message button, register Receiver button and logout receiver button.
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <ButtonAndroid:id= "@+id/btn_send_receiver"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Send Message" /> <ButtonAndroid:id= "@+id/btn_register_receiver"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Register receiver" /> <ButtonAndroid:id= "@+id/btn_unregister_receiver"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Logoff receiver" /></LinearLayout>
3, the final Test in the mainactivity
1 Public classMainactivityextendsActivityImplementsOnclicklistener {2 PrivateMyreceiver receiver =NULL;3 @Override4 protected voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7Findviewbyid (R.id.btn_send_receiver). Setonclicklistener ( This);8Findviewbyid (R.id.btn_register_receiver). Setonclicklistener ( This);9Findviewbyid (R.id.btn_unregister_receiver). Setonclicklistener ( This);Ten } One A @Override - Public voidOnClick (View v) { - Switch(V.getid ()) { the CaseR.id.btn_send_receiver: -Intent i =NewIntent (myreceiver.action); -I.putextra ("Data", "messages from Mainactivity"); - Sendbroadcast (i); + Break; - CaseR.id.btn_register_receiver: + if(Receiver = =NULL) { ASYSTEM.OUT.PRINTLN ("Register receiver"); atReceiver =Newmyreceiver (); -Registerreceiver (receiver,NewIntentfilter (myreceiver.action)); - } - Break; - CaseR.id.btn_unregister_receiver: - if(Receiver! =NULL) { inSYSTEM.OUT.PRINTLN ("Logout receiver"); - Unregisterreceiver (receiver); toReceiver =NULL; + } - Break; the * default: $ Break;Panax Notoginseng } - } the}
Code Analysis: This section of code in 23 lines, through the Registerreceiver () method to dynamically register the Myreceiver, and it is best not to forget to use the Unregisterreceiver () method to unregister it, The receiver should be set to null after logging off.
4. The result of operation is as follows
The test will find that before clicking the Send Message button, if you do not register receiver, you cannot send the message successfully, only register receiver, click Send Message log to output:
From the log output can be seen, when the receiver is registered, press the Send Message button, in the Myreceiver can receive messages from Mainactivity.
Android Learning Note (11) Broadcastreceiver Dynamic registration, logoff example