Joke Collection Website - Blessing messages - Broadcast (broadcast)

Broadcast (broadcast)

Broadcast is a widely used mechanism to transfer messages between applications, and it is one of the four components of Android system. The broadcasting mechanism consists of three basic elements: broadcasting, which is used to send broadcasts; A broadcast receiver for receiving a broadcast; Intent, a medium for storing broadcast related information.

Broadcasting is divided into two aspects: broadcasting sender and broadcasting receiver. In the Android system, many operations will be sent after completion, such as sending text messages, making phone calls, turning on the phone, or changing the network status and power supply.

Broadcasting is divided into disorderly broadcasting (ordinary broadcasting) and orderly broadcasting.

This is a completely asynchronous and efficient broadcast. After broadcasting, all broadcast receivers may even receive broadcast messages at the same time, so there is no order between them. Unordered broadcasts cannot be intercepted, terminated or modified.

This is a synchronous broadcast. After the broadcast is sent out, only one broadcast receiver can receive the broadcast message at the same time, and the broadcast will continue to be delivered after the logic in this broadcast receiver is executed. There are a series of broadcast receivers. A broadcast receiver with high priority can receive the broadcast message first, and the previous broadcast receiver can also intercept the broadcast being transmitted, so that the latter broadcast receiver cannot receive the broadcast message.

Broadcast registration can be divided into static registration and dynamic registration.

Add it to the AndroidManifest.xml file.

broadcast receiver

Register the broadcast in onCreate () of Activity.

Unregister the broadcast in the active onDestroy ().

The life cycle of broadcasting is only about ten seconds. After this time, it will be reported to the application instead of the response (ANR). If you want to do a time-consuming operation, you should send your intention to the service and let it complete. Broadcast can't be solved by sub-thread, because the life cycle of BroadcastReceiver is short, and the broadcast may end before the sub-thread ends.