There are roughly two types of broadcasts in Android: ordered broadcasts and standard broadcasts. A standard broadcast is a fully asynchronous broadcast that, after broadcast, is received by the broadcast receiver almost at the same time, so there is no sequential order in which the broadcast is more efficient to perform, but it is thought that it cannot be truncated. An ordered broadcast is a synchronous broadcast that, once broadcast, only one broadcast receiver receives the broadcast at the same time, and the message continues to propagate when the logic in the broadcast receiver is completed. And the previous broadcast can truncate the broadcast being propagated.
Here is an example of a standard broadcast:
1. First create a receive class to inherit the Broadcastreciver class:
Package Org.lxh.demo;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.widget.toast;public class Mybroadcastreceiver extends BroadcastReceiver {@ overridepublic void OnReceive (context context, Intent Intent) {toast.maketext (context, "received in Mybroadcastreceiver ", Toast.length_short). Show ();}}
2. Configure the Androidmanifest.xml file:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Org.lxh.demo "android:versioncode=" 1 "android:versionname=" 1.0 "><USES-SDK android: minsdkversion= "/><application android:icon=" @drawable/icon "android:label=" @string/app_name ">< Activity android:name= ". Hello "android:label=" @string/app_name "><intent-filter><action android:name=" Android.intent.action.MAIN "/><category android:name=" Android.intent.category.LAUNCHER "/></ Intent-filter></activity><receiver android:name= "Org.lxh.demo.MyBroadcastReceiver" > < intent-filter> <action android:name= "Com.example.broadcast.MY_BROADCASTTEST"/> </ Intent-filter></receiver> </application></manifest>
That
<receiver android:name= "Org.lxh.demo.MyBroadcastReceiver" > <intent-filter> <action Android:name= "Com.example.broadcast.MY_BROADCASTTEST"/> </intent-filter></receiver>
3. Send program:
Package Org.lxh.demo;import Android.app.activity;import Android.app.alertdialog;import android.app.Dialog;import Android.content.dialoginterface;import Android.content.intent;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.view.window;import Android.widget.button;import Android.widget.edittext;import Android.widget.TextView; public class Hello extends Activity {private Button btn=null;public void OnCreate (Bundle savedinstancestate) {Super.oncre Ate (savedinstancestate); Life cycle Method Requestwindowfeature (Window.feature_no_title); Super.setcontentview (R.layout.main); Set the layout manager this.btn= (Button) Super.findviewbyid (R.ID.MYBTN) to use, This.btn.setOnClickListener (new Clicklistenerimpl ( ));} Private class Clicklistenerimpl implements Onclicklistener{public void OnClick (View arg0) {Intent intent=new Intent (" Com.example.broadcast.MY_BROADCASTTEST "); Sendbroadcast (intent);}}
Run as follows:
Let's create a broadcast receive class:
Package Org.lxh.demo;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.widget.toast;public class Anotherbroadcastreceiver extends BroadcastReceiver { @Overridepublic void OnReceive (context context, Intent Intent) {toast.maketext (context, "received in Anotherbroadcastreceiver ", Toast.length_short). Show ();}}
Once again, configure the Androidmanifest.xml file:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Org.lxh.demo "android:versioncode=" 1 "android:versionname=" 1.0 "><USES-SDK android: minsdkversion= "/><application android:icon=" @drawable/icon "android:label=" @string/app_name ">< Activity android:name= ". Hello "android:label=" @string/app_name "><intent-filter><action android:name=" Android.intent.action.MAIN "/><category android:name=" Android.intent.category.LAUNCHER "/></ Intent-filter></activity><receiver android:name= "Org.lxh.demo.MyBroadcastReceiver" > < intent-filter> <action android:name= "Com.example.broadcast.MY_BROADCASTTEST"/> </INTENT-FILTER>&L T;/receiver><receiver android:name= "Org.lxh.demo.AnotherBroadcastReceiver" > <intent-filter> <a ction android:name= "Com.example.broadcast.MY_BROADCASTTEST"/> </intent-filter></receiver></application></manifest>
Two broadcast receivers filter the same broadcast, so they will receive this broadcast at the same time, running as follows:
That is, the first registered broadcast is displayed, followed by the second one. How do we consider changing the order of reception? You can set the priority <intent-filter android:priority= "" ";:
<receiver android:name= "Org.lxh.demo.MyBroadcastReceiver" > <intent-filter> <action Android:name= "Com.example.broadcast.MY_BROADCASTTEST"/> </intent-filter></receiver>< Receiver android:name= "Org.lxh.demo.AnotherBroadcastReceiver" > <intent-filter android:priority= "100" > <action android:name= "Com.example.broadcast.MY_BROADCASTTEST"/> </intent-filter></ Receiver>
The order in which the receive is run is reversed. Consider how to not let the second receiver receive a broadcast? We can then write Abortbroadcast () in the first receiver, i.e.:
Package Org.lxh.demo;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.widget.toast;public class Anotherbroadcastreceiver extends BroadcastReceiver { @Overridepublic void OnReceive (context context, Intent Intent) {toast.maketext (context, "received in Anotherbroadcastreceiver ", Toast.length_short). Show (); Abortbroadcast ();}}
At this point the operation will error:
04-22 01:48:58.187:e/broadcastreceiver (435): Broadcastreceiver trying to return result during a non-ordered broadcast
04-22 01:48:58.187:e/broadcastreceiver (435): Java.lang.RuntimeException:BroadcastReceiver trying to return result During a non-ordered broadcast
04-22 01:48:58.187:e/broadcastreceiver (435): At Android.content.BroadcastReceiver.checkSynchronousHint ( broadcastreceiver.java:451)
04-22 01:48:58.187:e/broadcastreceiver (435): At Android.content.BroadcastReceiver.abortBroadcast ( broadcastreceiver.java:374)
04-22 01:48:58.187:e/broadcastreceiver (435): At Org.lxh.demo.AnotherBroadcastReceiver.onReceive ( ANOTHERBROADCASTRECEIVER.JAVA:14)
04-22 01:48:58.187:e/broadcastreceiver (435): at Android.app.ActivityThread.handleReceiver (Activitythread.java : 1794)
04-22 01:48:58.187:e/broadcastreceiver (435): at android.app.activitythread.access$2400 (ActivityThread.java:117)
04-22 01:48:58.187:e/broadcastreceiver (435): at Android.app.activitythread$h.handlemessage (ActivityThread.java : 981)
04-22 01:48:58.187:e/broadcastreceiver (435): at Android.os.Handler.dispatchMessage (handler.java:99)
04-22 01:48:58.187:e/broadcastreceiver (435): at Android.os.Looper.loop (looper.java:123)
04-22 01:48:58.187:e/broadcastreceiver (435): at Android.app.ActivityThread.main (activitythread.java:3683)
04-22 01:48:58.187:e/broadcastreceiver (435): At Java.lang.reflect.Method.invokeNative (Native Method)
04-22 01:48:58.187:e/broadcastreceiver (435): at Java.lang.reflect.Method.invoke (method.java:507)
04-22 01:48:58.187:e/broadcastreceiver (435): At Com.android.internal.os.zygoteinit$methodandargscaller.run ( zygoteinit.java:839)
04-22 01:48:58.187:e/broadcastreceiver (435): at Com.android.internal.os.ZygoteInit.main (zygoteinit.java:597)
04-22 01:48:58.187:e/broadcastreceiver (435): At Dalvik.system.NativeStart.main (Native Method)
Tip This is a broadcast without order. How to correct it? Simply change the program that sent the broadcast to Sendorderedbroadcast (intent, NULL);:
Package Org.lxh.demo;import Android.app.activity;import Android.app.alertdialog;import android.app.Dialog;import Android.content.dialoginterface;import Android.content.intent;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.view.view.onfocuschangelistener;import Android.view.window;import Android.widget.button;import Android.widget.edittext;import Android.widget.TextView; public class Hello extends Activity {private Button btn=null;public void OnCreate (Bundle savedinstancestate) {Super.oncre Ate (savedinstancestate); Life cycle Method Requestwindowfeature (Window.feature_no_title); Super.setcontentview (R.layout.main); Set the layout manager this.btn= (Button) Super.findviewbyid (R.ID.MYBTN) to use, This.btn.setOnClickListener (new Clicklistenerimpl ( ));} Private class Clicklistenerimpl implements Onclicklistener{public void OnClick (View arg0) {Intent intent=new Intent (" Com.example.broadcast.MY_BROADCASTTEST ");//sendbroadcast (intent); Sendorderedbroadcast (Intent, NULL);}}}
When you run the instance again, only the first toast is displayed:
and will not error.
A study of android--broadcasting