4 components of the Android system finally, there is a component. Broadcastreceiver, the component is a global listener , able to listen to the system's global broadcast messages, can easily realize the communication between different components of the system
Broadcastreceiver has its own process, system-level listener, only to exist with the matching intent is broadcast,Broadcastreceiver will be excited
To create your own Broadcastreceiver object, we need to inherit android.content.BroadcastReceiver and implement its OnReceive method
Myreceiver.java
public class Myreceiver extends broadcastreceiver{@Overridepublic void onreceive (context context, Intent Intent) { Toast.maketext (Context, "The received intent action is:" + intent.getaction () + "\ n message content is:" + Intent.getstringextra ("msg"), Toast.length_long). Show ();}}
Receiver for manifest.xml manifest file configuration
<receiver android:name= ". Myreceiver "><intent-filter><action android:name=" Org.crazyit.action.CRAZY_BROADCAST "/></ Intent-filter></receiver>
This means that regardless of which component the action of intent is "Org.crazyit.action.CRAZY_BROADCAST" and broadcast using Sendbroadcast (intent), then Myreceiver is started
public class Broadcastmain extends Activity{button send, @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//Get Buttonsend = (Button) Findviewbyid in the program interface ( R.id.send); Send.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Create intent Object Intent Intent = new Intent ();//Set the Action property of the intent Intent.setaction ("Org.crazyit.action.CRAZY_BROADCAST"); Intent.putextra (" Msg "," simple Message ");//Send Broadcast sendbroadcast (intent);}});}}
There are two ways to register receiver:
Static Brochure
The static register is configured in the Androidmanifest.xml file. We're here to register a broadcast address for myreceiver:
<receiver android:name= ". Myreceiver "> <intent-filter> <action android:name=" Android.intent.action.MY_BROADCAST "/> <category android:name= "Android.intent.category.DEFAULT"/> </intent-filter>
after you have configured the above information. Only if Android.intent.action.MY_BROADCAST the address of the broadcast, Myreceiver can receive. Note that such a way of registration is resident type. In other words, when the application is closed, if there is a broadcast message, Myreceiver will be called by the system and self-executing.
Dynamic Brochure
Dynamic registration requires a dynamic designation of the broadcast address in the code and a registration, usually we are in the activity or service register a broadcast, the following we look at the code of the Register:
Myreceiver receiver = new Myreceiver (); Intentfilter filter = new Intentfilter (); Filter.addaction ("Android.intent.action.MY_BROADCAST"); Registerreceiver (receiver, filter); Brochure <pre name= "code" class= "Java" >receiver and filter
General Broadcast
Ordinary broadcasts are completely asynchronous for multiple receivers. Typically, each recipient does not have to wait to receive the broadcast, and the recipient has no effect on each other.
For such a broadcast. The receiver cannot terminate the broadcast. It is impossible to prevent other receivers from receiving actions.
The example above is the normal broadcast that is sent
Orderly broadcast
The ordered broadcast is special, it is sent only to the higher priority recipients, and then by the high-priority recipients to the lower priority recipients . Recipients with high priority have the ability to terminate the broadcast.
For example: Priority A>b>c,broadcast is passed first to a. and pass it on to B, to C. Priority level declaration <Intent-filter> element Android:priority. The greater the number, the higher the value range in -1000~1000
Recipients receiving broadcast are given the ability to deposit processing results into broadcast through the Setresultextras (Bundle) method and pass them on to the next recipient. The data deposited by the previous recipient is obtained through Bundle Bunde=getresultextras (True)
public class Sortedbroadcast extends Activity{button send, @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//Get sendbuttonsend in program = (Button) Findviewbyid ( R.id.send); Send.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Create intent Object Intent Intent = new Intent (); Intent.setaction ("Org.crazyit.action.CRAZY_BROADCAST"); Intent.putextra ("msg", "simple Message");// Send ordered broadcast sendorderedbroadcast (intent, NULL);}});}}
Myreceiver.java
public class Myreceiver extends broadcastreceiver{@Overridepublic void onreceive (context context, Intent Intent) { Toast.maketext (Context, "The received intent action is:" + intent.getaction () + "\ n message content is:" + Intent.getstringextra ("msg"), Toast.length_long). Show ();//Create a Bundle object and deposit the data bundle bundle = new bundle (); Bundle.putstring ("First", " The first broadcastreceiver the message ");//The bundle is put into the result Setresultextras (bundle);//Cancel the continuation of broadcast//Abortbroadcast (); ①}}
Myreceiver2.java
public class MyReceiver2 extends broadcastreceiver{@Overridepublic void onreceive (context context, Intent Intent) { Bundle bundle = Getresultextras (true);//parse the message that was deposited by the previous broadcastreceiver. String first = Bundle.getstring "); Toast.maketext (Context, "the first broadcast to deposit the message is:" + primary, Toast.length_long). Show ();}}
manifest file
<receiver android:name= ". Myreceiver "><intent-filter android:priority=" ><action android:name= "org.crazyit.action.CRAZY_ Broadcast "/></intent-filter></receiver><receiver android:name=". MyReceiver2 "><intent-filter android:priority=" 0 "><action android:name=" org.crazyit.action.CRAZY_ Broadcast "/></intent-filter></receiver>
Specific explanations for broadcastreceiver components in Android