The main uses of Broadcastreceiver in Android are
Send notifications, update UI or data, communicate with each other, monitor system status (e.g. boot, network, etc.)
How to register Broadcasetreceiver in Android
Manifest global registrations in the manifest file
Use code registration in service or activity according to life cycle
How to register Manifest
<receiver android:name= "Com.sample.test.MyBroadcastReciever" > <intent-filter> &L T;action android:name= "Com.sample.test.ACTION_DO_SOMETHING" ></action> <action android:name= "and Roid.intent.ACTION_WIFI_STATE_CHANGED "></action> </intent-filter> </receiver>
Registering with Code
Sampleactivity
Private Myreceiver receiver, @Override public void OnStart () {Super.onstart (); Receiver = new Myreceiver (); Intentfilter filter = new Intentfilter (); Filter.addaction ("Android.intent.action.MY_BROADCAST"); Registerreceiver (receiver, filter);} @Overridepublic void OnStop () {super.onstop (); Unregisterreceiver (receiver); }
How to send broadcasts in Android
Normal broadcast: Regardless of priority size, will be sent to all listening action= "Com.test.sample.action" broadcast, the content can not be modified, non-transitive.
Intent Intent = new Intent ("Com.test.sample.action"); Sendbroadcast (Intent);
Asynchronous (sticky) broadcasts: when the intent after processing, still exist, this time Registerreceiver (Broadcastreceiver, Intentfilter) can also receive his value, until you remove it , no transitivity , cannot terminate (abort ()) broadcast.
Send this broadcast requires permission <uses-permission android:name="Android.permission.BROADCAST_STICKY" />
To remove is to use this method Removestickybroadcast (intent); But do not forget in the implementation of this method of application inside Androidmanifest.xml also have to add face permissions;
Sendstickyorderedbroadcast (Intent, Resultreceiver, Scheduler, Initialcode, Initialdata, Initialextras)
Orderly broadcast:
Receive broadcasts in the recipient's priority order, which is declared in priority in Intent-filter, between 1000 and 1000, and the higher the value, the greater the priority level. The continuation of broadcast intent can be terminated, and the receiver can tamper with the content and be transitive.
Sendbroadcast (Intent);
The Broadcastreceiver in Android can be used to send messages to another broadcast, which can be achieved through programs or processes.
The above review of the Android broadcast use case, generally not very good security, so only for less secure data delivery, or page updates.
The introduction of Localbroadcastmanager in Android-support-v4.jar, known as the local notification Manager, has the advantage of high security and high efficiency, suitable for local communication and can be used instead of handler to update the UI
Public class localservicebroadcasteractivity extends activity {static final String ACTION_STARTED = "Com.example.android.supportv4.STARTED";static final string action_update = "Com.example.android.supportv4.UPDATE"; static final string ACTION_STOPPED = "Com.example.android.supportv4.STOPPED"; localbroadcastmanager mlocalbroadcastmanager; broadcastreceiver mreceiver; @Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); final textview callbackdata = (TextView) findviewbyid (r.id.callback); Callbackdata.settext ("no broadcast received yet "); Mlocalbroadcastmanager = localbroadcastmanager.getinstance (this);IntentFilter Filter = new intentfilter (); filter.addaction (action_started); filter.addaction (ACTION_UPDATE); Filter.addaction (ACtion_stopped); Mreceiver = new broadcastreceiver () {@Overridepublic void OnReceive (context context, intent intent) {if (Intent.getaction (). Equals (Action_ STARTED)) {callbackdata.settext ("STARTED");} else if (Intent.getaction () equals (action_update)) {callbackdata.settext ("Got update : " + intent.getintextra (" value ", 0));} else if (Intent.getaction () equals (action_stopped)) {callbackdata.settext ("STOPPED");}}; Mlocalbroadcastmanager.registerreceiver (Mreceiver, filter); button button = (Button) findviewbyid (R.id.start); Button.setonclicklistener (MStartListener );button = (Button) findviewbyid (r.id.stop); Button.setonclicklistener (Mstoplistener);} @Overrideprotected void ondestroy () {super.ondestroy (); Mlocalbroadcastmanager.unregisterreceiver (mreceiver);} Private onclicklistener mstartlistener = new&nBsp;onclicklistener () {public void onclick (view v) {startservice (New Intent ( Localservicebroadcasteractivity.this, localservice.class));}}; Private onclicklistener mstoplistener = new onclicklistener () {public void onclick (view v) {stopservice (New intent (localservicebroadcasteractivity.this, Localservice.class));}}; public static class localservice extends service {localbroadcastmanager mlocalbroadcastmanager;int mcurupdate;static final int msg_update = 1; Handler mhandler = new handler () {@Overridepublic void handlemessage ( message msg) {switch (msg.what) {case MSG_UPDATE: {mCurUpdate++;Intent Intent = new intent (action_update); Intent.putextra ("value", mcurupdate); Mlocalbroadcastmanager.sendbroadcast (Intent); Message nmsg = mhandlEr.obtainmessage (msg_update); mhandler.sendmessagedelayed (nmsg, 1000);} Break;default:super.handlemessage (msg);}}; @Overridepublic void oncreate () {super.oncreate ();mlocalbroadcastmanager = Localbroadcastmanager.getinstance (this);} Public int onstartcommand (Intent intent, int flags, int startid) {// Tell any local interested parties about the Start.mLocalBroadcastManager.sendBroadcast (New intent (action_started));// prepare to do update reports.mhandler.removemessages (msg_update); Message msg = mhandler.obtainmessage (msg_update); mhandler.sendmessagedelayed (msg, 1000); Return servicecompat.start_sticky;} @Overridepublic void ondestroy () {super.ondestroy ();// tell any local Interested parties about the stop.mlocalbroadcastmanager.sendbroadcast (New Intent ( action_stopped));// stop doing updates.mhandler.removemessages (msg_update);} @Overridepublic ibinder onbind (intent intent) {return null;}}
How to use Localbroadcastmanager in Android