Android has a very important feature, is broadcast. You can also consider broadcasting as a communication mechanism. Android Four components: Activity, Service, Broadcastreceiver and ContentProvider, only activity and service have a complete life cycle, other Broadcastreceiver and ContentProvider are not. Broadcastreceiver is essentially a listener responsible for monitoring the broadcast (broadcast) emitted by the system application.
Broadcastreceiver has a onreceive (context context, Intent Intent) method, which is where broadcast receivers Broadcastreceiver receive broadcasts, Intent can be seen as a specific broadcast medium. Different intent will trigger different broadcast receivers, and then what to do is put in OnReceive () (ellipsis) to implement.
Send broadcast
It says that intent is a "broadcast medium" and puts intent in the broadcast method to be OK:
public class Broadcasttest extends Activity{private Button btn_send; @Overridepublic void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.id.act_main); btn_send = (Button) Findviewbyid (R.id.btn_send); Btn_send.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Send broadcast Sendmybroadcast ();}});} private void Sendmybroadcast () {Intent Intent = new Intent ();//Set Intent's Action property, This will enable intent to find the corresponding broadcast receiver intent.setaction ("Com.test.action.My_Test_BroadCast");//Set the transitive message Intent.putextra for intent (" Msgkey "," message content ");//Send Broadcast sendbroadcast (intent);}}
Receiving broadcasts (registering a broadcast receiver)1. Register in code to configure the broadcast receiver
Registering a broadcast receiver here, in conjunction with the above example of sending a broadcast, it will be good to understand some.
Call Broadcastreceiver's Registerreceiver (broadcastreceiver receiver, Intentfilter filter) in the code method to specify a specific intent broadcast corresponding to a specific broadcast receiver:
Fill in the contents of the above send Broadcast action content intentfilter filter = new Intentfilter ("Com.test.action.My_Test_BroadCast"); Myreceiver is a broadcast receiver that inherits the Broadcastreceiver custom implementation myreceiver,//It is best to do so in order to differentiate between different broadcast receivers used to correspond to different broadcast myreceiver receiver = new Myreceiver ();//Registration is complete, the Myreceiver broadcast receiver is now the corresponding receive action for "Com.test.action.My_Test_BroadCast" intent broadcast registerreceiver ( receiver, filter);
2. Register the configuration in the Androidmanifest.xml file
<receiver android:name= "Com.test.MyReceiver" ><intent-filter><!--specify myreceiver corresponding action as " Com.test.action.My_Test_BroadCast "Intent broadcast--><action android:name=" Com.test.action.My_Test_BroadCast " ></intent-filter></receiver>
Both of the above methods can be registered Myreceiver this broadcast receiver specifically responds to intent broadcasts with action "Com.test.action.My_Test_BroadCast". Once a broadcast is sent using Sendbroadcast () in a send broadcast, the myreceiver responds and receives processing in OnReceive ().
Emphasize some points
1, many people use in the jump activity when used intent to pass some content, if intent did not find the corresponding Activity.class will be error class can not be found such errors. But on the radio, Use intent to transmit broadcasts, even if intent does not find the corresponding broadcast receiver, but does not error. Over time, the broadcast will be destroyed by the system itself.
2, every broadcast, broadcast every time you find the broadcast receiver Broadcastreceiver, will create a new Broadcastreceiver instance, I will not say that we have used this broadcastreceiver instance before and continue to follow the old Broadcastreceiver instance. Because each time the Broadcastreceiver instance is in OnReceive () The instance is destroyed when the broadcast is received and processed. Since none exists, the next time the same broadcast is sent, the new Broadcastreceiver instance will continue to be created.
3, do not put the time-consuming operation in the OnReceive (), remember! The OnReceive () of the Broadcastreceiver instance is only suitable for some very lightweight operations, such as a "network outage" of the box tip, such as some simple data processing, and so on. Because more than a certain time (the old version of this limit is 10 seconds, the new version seems shorter), if the OnReceive () method is not finished, the system will be forced to destroy this instance, or even error.
4, if a broadcast receiver is registered in the activity, the broadcast receivers must be unregistered before exiting the activity. Similar in OnDestroy ():
@Overridepublic void OnDestroy () {//unregister getapplicationcontext (). Unregisterreceiver (receiver); Super.ondestroy ();}
5, distinguish between ordinary broadcast and orderly broadcast. It's all about ordinary broadcasting, and it's common to send broadcasts and receive broadcasts that are not synchronous, that is, asynchronous. How do you send it is your thing, people broadcast receiver How to receive processing is not related to sending broadcasts, so there are several broadcast at the same time, the first sent may be later to be responded to, Subsequent broadcasts are likely to be responded to sooner. While ordered broadcast is special, it distinguishes the priority of the broadcast receiver, the broadcast will first send to the high priority, then to the last priority of the broadcast receiver. And the high-priority receivers have the ability to end the broadcast so that the broadcast is no longer passed to the receiver with a lower priority than its own.
For an orderly broadcast, the last example:
First add permissions in Androidmanifest.xml:
Define 3 broadcast receivers:
public class A_receiver extends Broadcastreceiver {@Override public void onreceive (context context, I Ntent intent) {String msg = Intent.getstringextra ("key"); Here the bundle can be used to pass some information to the lower priority receiver Bundle bundle = new bundle (); Bundle.putstring ("Msgkey_a", msg); Setresultextras (bundle); If the last line joins Abortbroadcast (), the broadcast will no longer propagate//abortbroadcast () down one receiver; }} public class B_receiver extends Broadcastreceiver {@Override public void onreceive (context context, Inte NT Intent) {String msg = Getresultextras (True). GetString ("Msgkey_a"); Ditto bundle bundle = new bundle (); Bundle.putstring ("Msgkey_b", msg); Setresultextras (bundle); }} public class C_receiver extends Broadcastreceiver {@Override public void onreceive (context context, Inte NT Intent) {String msg = Getresultextras (True). GetString ("Msgkey_b"); Do other processing ...} }
Register the broadcast receiver below:
<receiver android:name= "Com.test.A_Receiver" > <!--priority= "xxx" The higher the value, the higher the priority, the value of two receivers is not as large as the range of 1000 to 1000-- > <intent-filter android:priority= "<action" > Android:name= "Android.intent.action.MY_TEST_BROAD CAST "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-filter> < ;/receiver> <receiver android:name= "Com.test.B_Receiver" > <intent-filter android:priority= "the" > <action android:name= "Android.intent.action.MY_TEST_BROADCAST"/> <category android:name= "Android . Intent.category.DEFAULT "/> </intent-filter> </receiver> <receiver android:name=" com.tes T.c_receiver "> <intent-filter android:priority=" 98 "> <action android:name=" android.intent.action . My_test_broadcast "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-filter > </receiver>
Send an ordered broadcast:
private void Sendbroadcast () {Intent Intent = new Intent ("Android.intent.action.MY_TEST_BROADCAST"); Intent.putextra ("Key", "Hello receiver."); Sendorderedbroadcast (Intent, "Scott.permission.MY_BROADCAST_PERMISSION"); }
The broadcast mechanism is a very important part of the Android system and is used together with the service to enable a powerful backend. Make your application smoother and more efficient,
Android four components of the broadcast receiver Broadcastreceiver