Broadcasting is mainly divided into two types, standard broadcasting and orderly broadcasting, which is a practical way to look at the specific differences between the two broadcasts.
First, send standard broadcast
Before sending the broadcast, we still need to define a broadcast receiver to prepare to receive this broadcast, otherwise it is white hair. So create a new mybroadcastreceiver that inherits from Broadcastreceiver, and the code looks like this:
Public class extends broadcastreceiver { @Override publicvoid onreceive (context context, Intent Intent) { "received in Mybroadcastreceiver", toast.length_short). Show (); }}
Here, when Mybroadcastreceiver receives a custom broadcast, it pops up the received in Mybroadcastreceiver prompt. This broadcast receiver is then registered in Androidmanifest.xml:
<manifest xmlns:android= "http://schemas.android.com/apk/res/android" Package = "Com.example.broadcasttest" Android:versioncode = "1" Android:versionname = "1.0" > ... <application Android:allow Backup = "true" Android:icon =" @drawable/ic_launcher " Android:label = "@string/app_name" Android:theme = "@style/appthem E "> ... <receiver android:name=. Mybroadcastreceiver "> <intent-filter> <action android:name=" Com.example.broadcastte St. My_broadcast "/> </intent-filter> </receiver> </application></ma Nifest>
As you can see, this lets Mybroadcastreceiver receive a value of com.example.broadcasttest. My_broadcast, so we're going to need to send out a broadcast in a moment.
Next, modify the code in Activity_main.xml as follows:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <ButtonAndroid:id= "@+id/button"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Send broadcast" /></LinearLayout>
Here, a button is defined in the layout file that is used as the trigger point for sending the broadcast. Then modify the code in the mainactivity as follows:
Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent ("Com.example.broadcasttest. My_broadcast"); Sendbroadcast (Intent); } }); }}
As you can see, we have added logic for sending custom broadcasts to the button's Click event. First, a intent object is constructed, the value of the broadcast to be sent is passed in, and then the Sendbroadcast () method of the context is called to send the broadcast, so that all the listening com.example.broadcasttest.MY_ Broadcast the broadcast receiver will receive a message. The broadcast that is issued at this time is a standard broadcast.
Run the program and click on the Send Broadcast button to effect.
This allows us to successfully complete the function of sending a custom broadcast. In addition, because broadcasts are delivered using intent, you can also carry some data to the broadcast receiver in intent.
Second, send an orderly broadcast
Broadcasting is a way of communicating across processes, as you can see from the front of the receiving system broadcast. So the broadcast that is sent within our application should also be received by other applications. To verify this, we need to create a new BroadcastTest2 project.
Once the project is created, you will also need to define a broadcast receiver under this project to receive the custom broadcasts from the previous section. The new anotherbroadcastreceiver inherits from Broadcastreceiver, and the code looks like this:
Public class extends broadcastreceiver { @Override publicvoid onreceive (context context, Intent Intent) { "received in Anotherbroadcastreceiver", toast.length_short). Show (); }}
Here is still a text message popped out of the OnReceive () method of the broadcast receiver. The broadcast receiver is then registered in Androidmanifest.xml, as shown in the following code:
<Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.broadcasttest2"Android:versioncode= "1"Android:versionname= "1.0" > <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <receiverAndroid:name=". Anotherbroadcastreceiver " > <Intent-filter> <ActionAndroid:name= "Com.example.broadcasttest.MY_BROADCAST" /> </Intent-filter> </receiver> </Application></Manifest>
As you can see, Anotherbroadcastreceiver also receives the com.example.broadcasttest. My_broadcast the radio. Now run the BROADCASTTEST2 project to install the program on the emulator, and then back to the main interface of the Broadcasttest project, and click on the Send Broadcast button, will pop up two times the prompt message, respectively.
This is a strong proof that the broadcasts that our application emits can be received by other applications.
But so far, the program has been issued by the standard broadcast, now let's try to send an orderly broadcast. Close the BroadcastTest2 project, and then modify the code in the mainactivity as follows:
Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button Button=(Button) Findviewbyid (R.id.button); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent ("Com.example.broadcasttest. My_broadcast"); Sendorderedbroadcast (Intent,NULL); } }); }}
As you can see, sending an ordered broadcast requires only one line of code to change and the Sendbroadcast () method is changed to the Sendorderedbroadcast () method. The Sendorderedbroadcast () method receives two parameters, the first parameter is still intent, and the second parameter is a permission-related string, where null is passed. Now rerun the program and click on the Send Broadcast button and you will find that two applications can still receive this broadcast.
It seems to be no different from the standard broadcast, but don't forget that this time the broadcast receivers are sequential, and the front broadcast receiver can also truncate the broadcast to prevent it from continuing to propagate.
So how do you set the order of the broadcast receivers? Of course it was set at the time of registration, modify the code in Androidmanifest.xml as follows:
<Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.broadcasttest"Android:versioncode= "1"Android:versionname= "1.0" > <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <receiverAndroid:name=". Mybroadcastreceiver " > <Intent-filterandroid:priority= "+" > <ActionAndroid:name= "Com.example.broadcasttest.MY_BROADCAST" /> </Intent-filter> </receiver> </Application></Manifest>
As you can see, we set the priority for the broadcast receiver via the Android:priority property, and the broadcast receiver with the higher priority can receive the broadcast first. The priority of Mybroadcastreceiver is set to 100 to ensure that it will receive broadcasts before anotherbroadcastreceiver.
Now that the priority has been given to receiving the broadcast, the Mybroadcastreceiver can choose whether to allow the broadcast to continue to pass. Modify the code in the Mybroadcastreceiver as follows:
Public class extends broadcastreceiver { @Override publicvoid onreceive (context context, Intent Intent) { "received in Mybroadcastreceive", toast.length_short). Show (); Abortbroadcast (); }}
If the Abortbroadcast () method is called in the OnReceive () method, it means that the broadcast is truncated and subsequent broadcast receivers will no longer be able to receive the broadcast. Now rerun the program and click on the Send Broadcast button, and you will notice that only the toast information in the Mybroadcastreceiver can pop up, indicating that the broadcast was actually terminated after Mybroadcastreceiver.
android#boardcast# Sending a custom broadcast