Send orderly broadcast case code in Android
The Android system provides two types of broadcasting, one is orderly broadcasting and the other is orderly broadcasting.
(1) Unordered broadcasting is completely asynchronous. When sending a broadcast, all broadcast receivers listening to the broadcast will receive this message, but the receiving order is uncertain.
(2) Ordered broadcast is received according to the priority of the receiver. Only one broadcast receiver can receive information. After the logic execution in this broadcast receiver is completed, the transmission will continue.
Experimental requirements: send an ordered broadcast through sendorderedbroadcast()
1. In the activity-main.xml layout file, the code is as follows:
2. The code in mainactivity is as follows:
3. Create three broadcast recipients
(1)MyBroadcastReceiverOne
(2)MyBroadcastReceiverTwo
(3)MyBroadcastReceiverThree
(1) In mybroadcastreceiverone, the code is as follows:
(2) In mybroadcastreceivertwo, the code is as follows:
(3) In mybroadcastreceiverthree, the code is as follows:
4. In the configuration file androidmanifest, the code is as follows:
5. Experimental effect diagram:
After running the program, solve the following problems:
Question 1: after the program is started, click the "send ordered broadcast" button to send a broadcast event. At this time, observe the prompt information under the logcat window. What is output? Why?
After clicking the button, a prompt message as shown in the figure below appears. The reason is that if the orderly broadcast receives the broadcast information according to the priority, the priority value of the broadcast receiver one is the largest in the configuration file, so it receives the broadcast first, then three, and finally two. Android: priority = "value" is used to set the priority of broadcast recipients. The higher the value, the higher the priority.
Question 2: if the priority of the broadcast receiver mybroadcastreceivertwo is also set to 1000, and mybroadcastreceivertwo is registered in front of mybroadcastreceiverone, then run the program and observe the results, what conclusion can you draw?
Modify the code in the configuration file androidmanifest as follows:
The running program displays the prompt message as shown in the figure below. The reason is that when the priority of the broadcast receiver is the same, the first registered broadcast receiver receives the broadcast first.
Question 3: modify mybroadcastreceivertwo as follows. What conclusions can you draw from the observation results?
The running program displays the prompt message as shown in the figure below. The reason is that the broadcast receiver two has the highest priority, so write an interceptor in two, and the broadcast receiver with low priority will not receive the broadcast information. Therefore, one and three do not receive the broadcast events, so they do not print the information about them.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.