Improving Android 6-broadcastreceiver

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

The previous sections discussed activity and service respectively. This is the turn of broastcastreceiver. broastcast is a means of inter-Application Communication. Broastcastreceiver is closely connected with intent. After broastcastreceiver is registered dynamically or statically, the system automatically starts the qualified broastcastreceiver after intent is sent using sendbroadcast, which is similar to the interruption of the embedded system.

This document describes how to register broastcastreceiver statically or dynamically, obtain power information from the system, and obtain enumeration information fields. This article runs as follows:

It is a broadcastreceiver that sends an intent to the internal dynamic registration. The message name is displayed after receiving it. Registerreceiver () is used to dynamically register broadcastreceiver ().

 

It is the broadcastreceiver that sends an intent to the internal static registration. The message name is displayed after receiving it. Static registration is more troublesome than dynamic registration. First, create a new class to inherit broadcastreceiver, and then add it to androidmanifest. xml.

<Cycler Android: Name = "clsreceiver2"> <br/> <intent-filter> <br/> <action <br/> Android: Name = "com. testbroadcastreceiver. internal_2 "/> <br/> </intent-filter> <br/> </Cycler>

The first name is the class name, and the second is the action name.

Is to enumerate intent message fields, this function is more suitable for lazy people, the intent message received all the fields are broken down, and then look at which needs, too lazy to remember. The code for implementing this part is as follows:

// If the unknown intent contains content, you need to use the following method to list <br/> bundle B = intent. getextras (); <br/> object [] lstname = B. keyset (). toarray (); </P> <p> for (INT I = 0; I <lstname. length; I ++) <br/>{< br/> string keyname = lstname [I]. tostring (); <br/> log. E (keyname, String. valueof (B. get (keyname); <br/>}

The main. XML Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </P> <p> <button Android: Id = "@ + ID/button01" Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: text = "sent to the Internally dynamically registered broadcastreceiver"> </button> <br/> <button Android: id = "@ + ID/button02" Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: TEXT = "send to internal static register broadcastreceiver"> </button> <br/> <button Android: Id = "@ + ID/button03" Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: TEXT = "sent to system broadcastreceiver"> </button> <br/> </linearlayout> <br/>

The code for testbroadcastreceiver. Java is as follows:

Package COM. testbroadcastreceiver; </P> <p> Import android. app. activity; <br/> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. content. intentfilter; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. toast; </P> <p> public class testbroadcastreceiver extends activity {<br/> button btninternal1, btninternal2, btnsystem; <br/> static final string intenal_action_1 = "com. testbroadcastreceiver. internal_1 "; <br/> static final string intenal_action_2 =" com. testbroadcastreceiver. internal_2 "; <br/> static final string intenal_action_3 =" com. testbroadcastreceiver. internal_3 "; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> btninternal1 = (button) This. findviewbyid (R. id. button01); <br/> btninternal1.setonclicklistener (New clickevent (); <br/> btninternal2 = (button) This. findviewbyid (R. id. button02); <br/> btninternal2.setonclicklistener (New clickevent (); <br/> btnsystem = (button) This. findviewbyid (R. id. button03); <br/> btnsystem. setonclicklistener (New clickevent (); <br/> // dynamically register a broadcast message <br/> registerreceiver (bcrintenal1, new intentfilter (intenal_action_1 )); <br/>}< br/> class clickevent implements view. onclicklistener {</P> <p> @ override <br/> Public void onclick (view v) {<br/> If (V = btninternal1) // send data to the dynamically registered broadcastreceiver <br/>{< br/> intent = new intent (intenal_action_1); <br/> sendbroadcast (intent ); <br/>}< br/> else if (V = btninternal2) // send data to the static registered broadcastreceiver <br/>{< br/> intent = new intent (intenal_action_2); <br/> sendbroadcast (intent ); <br/>}< br/> else if (V = btnsystem) // dynamically register the broadcastreceiver that receives two groups of Information <br/>{< br/> intentfilter filter = new intentfilter (); // <br/> filter. addaction (intent. action_battery_changed); // system power detection information <br/> filter. addaction (intenal_action_3); // the third group of custom messages <br/> registerreceiver (batinforeceiver, filter); </P> <p> intent = new intent (intenal_action_3 ); <br/> intent. putextra ("name", "hellogv"); <br/> intent. putextra ("blog", "http://blog.csdn.net/hellogv"); <br/> sendbroadcast (intent ); // transfer past <br/>}</P> <p>/* <br/> * receive Dynamic Registration Broadcast <br/> */<br/> private broadcastreceiver bcrintenal1 = new broadcastreceiver () {</P> <p> Public void onreceive (context, intent) {<br/> string action = intent. getaction (); <br/> toast. maketext (context, "dynamic:" + action, 1000 ). show (); <br/>}< br/>}; </P> <p> private broadcastreceiver batinforeceiver = new broadcastreceiver () {</P> <p> Public void onreceive (context, intent) {<br/> string action = intent. getaction (); <br/> // If the captured action is action_battery_changed <br/> If (intent. action_battery_changed.equals (Action) {<br/> // when the unknown intent contains content, you need to list it using the following method: <br/> bundle B = intent. getextras (); <br/> object [] lstname = B. keyset (). toarray (); </P> <p> for (INT I = 0; I <lstname. length; I ++) <br/>{< br/> string keyname = lstname [I]. tostring (); <br/> log. E (keyname, String. valueof (B. get (keyname ))); <br/>}< br/> // If the captured action is intenal_action_3 <br/> If (intenal_action_3.equals (Action )) {<br/> // If the unknown intent contains content, you must use the following method to list <br/> bundle B = intent. getextras (); <br/> object [] lstname = B. keyset (). toarray (); </P> <p> for (INT I = 0; I <lstname. length; I ++) <br/>{< br/> string keyname = lstname [I]. tostring (); <br/> log. E (keyname, B. getstring (keyname); <br/>}< br/>}; </P> <p>}

The code for clsreceiver2.java is as follows:

Package COM. testbroadcastreceiver; </P> <p> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. widget. toast; </P> <p>/* <br/> * receives the broadcastreceiver of the static registration broadcast. <br/> * Step 1: Go to androidmanifest. XML message registration here <br/> * <javaser Android: Name = "clsreceiver2"> <br/> <intent-filter> <br/> <action <br/> Android: name = "com. testbroadcastreceiver. internal_2 "/> <br/> </intent-filter> <br/> </Cycler> <br/> Step 2: Define the message string <br/> Step 3: send messages via intent to drive broadcastreceiver to trigger <br/> */<br/> public class clsreceiver2 extends broadcastreceiver {<br/> @ override <br/> Public void onreceive (context Context, intent intent) {<br/> string action = intent. getaction (); <br/> toast. maketext (context, "static:" + action, 1000 ). show (); </P> <p >}< br/>}

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.