The way of sending and registering broadcast in Android system and its advantages and disadvantages

In the Android system, there are two main ways to send and register broadcasts: Standard Broadcast (Normal Broadcast) and Ordered Broadcast (Ordered Broadcast), each of which has different advantages and disadvantages.

1. Standard broadcast (Normal Broadcast):
send broadcast through `sendBroadcast()` method, use `BroadcastReceiver` to register to receive broadcast. The sent broadcast will be delivered to all matching receivers in an unordered manner by the system.
advantage:

  • The efficiency of sending and receiving broadcasts is high, because the system will concurrently deliver broadcasts to all matching receivers without waiting for the processing results of the receivers.
  • Sending broadcasts is fast and will not block the sender's thread.

shortcoming:

  • There is no way to terminate the delivery of broadcasts, that is, there is no way to prevent other applications or components from receiving broadcasts.
  • There is no guarantee of the order in which broadcast receivers are executed, nor is there any way to know whether any receivers processed the broadcast.

2. Ordered Broadcast:
Send a broadcast through the `sendOrderedBroadcast()` method, and use `BroadcastReceiver` to register to receive the broadcast. The sent broadcasts will be delivered in the order of priority and receivers, and each receiver can terminate the broadcast or modify the content of the broadcast.
advantage:

  • The order in which broadcasts are delivered can be controlled to ensure that receivers process broadcasts in the expected order.
  • Receivers can terminate the delivery of the broadcast, preventing other receivers from receiving the broadcast.

shortcoming:

  • Sending and receiving broadcasts is less efficient because broadcasts are delivered sequentially to each receiver, and each receiver's processing time delays the delivery of the broadcast.
  • Sending broadcasts is slower because the sender needs to wait for each receiver to process the broadcast before continuing.

The way you choose to send and register broadcasts depends on your needs. If you want to send broadcasts quickly, and the order of broadcasts and the responses of other receivers is not important, then standard broadcasts are a simple and efficient choice. If you need to control the order in which broadcasts are delivered, or want receivers to be able to terminate the delivery of broadcasts, then ordered broadcasts are a more appropriate choice, although they are less efficient.

In addition, there is a local broadcast (Local Broadcast) method, which uses the `LocalBroadcastManager` class to send and register broadcasts. Local broadcasts are only delivered within the application and will not leave the context of the application, so they are safer and more efficient. It is suitable for communication between components within the application, and is faster and more reliable than standard broadcast and ordered broadcast.

Guess you like

Origin blog.csdn.net/m0_52537869/article/details/131094649