Broadcast receiver of Android system programming introduction series realizes inter process communication

In the previous articles on the two important components of Android system, interface activity is responsible for the interaction between applications and users, and service is responsible for the interaction between threads within applications or the data interaction between two application processes. It seems that these two components can meet the development needs of daily applications, but if the interaction between applications uses the Aidl specification in service, does each application itself have to declare some interfaces in other applications? This is very unfriendly for two applications belonging to different developers. Therefore, the Android system also provides a component called broadcast receiver boradcastreceiver, which adopts the broadcast mechanism to deliver and process notifications between two or more unknown applications.

As a way of inter process communication, the broadcast mechanism of Android system can also be divided into three aspects: communication message content, sender and receiver. The broadcast receiver broadcastreceiver component is only used by the receiver. In order to better understand the usage of broadcast receivers, the following three aspects of interprocess communication will be introduced respectively.

Similar to the interface activity or service, the communication message content between broadcasts is also data encapsulated with android.content.intent intent object. There are instructions for the use of intent objects in the interaction between interfaces.

As long as there is an android.content.context object in the context environment, you can call the sendbroadcast() series methods of the object to send a broadcast, and its usage is similar to the startactivity() series methods of the startup interface or the startservice() series methods of the startup service.

The most commonly used context. Sendbroadcast (intent) method can send a random notification, and the intent content sent by the current notification will be received among the registered broadcast recipients. Here, if there are multiple corresponding broadcast receivers, the current message content will be received in random order. The intent parameter is the instantiation object encapsulating the message content of the communication. The intent.setaction (string action) method must be called to set the unique identification behavior of the broadcast to be sent. The action here must be a unique string in the system as the flag for receiving the broadcast, The broadcast receiver below also declares an action of the same string to receive the current broadcast content.

You can also use the context. Sendorderedbroadcast (intent, string receiverpermission) method to send a notification received in sequence, and also receive the message content among the registered broadcast recipients, except that when there are multiple corresponding broadcast recipients, The message will be received in order of priority attribute value of the broadcast receiver at the time of registration. The message content will be received in random order only when two broadcast receivers have the same priority. Since a number of broadcast receivers are broadcast sequentially, abortBroadcast () can be interrupted in one of the broadcast receivers to interrupt the current broadcast so that it will not continue to transmit to the latter broadcast receiver.

In addition, in the officially recommended Android x library, you can use the local broadcast management class of android.localbroadcastmanager.content.localbroadcastmanager to call the localbroadcastmanager.getinstance (context). Sendbroadcast (intent) series methods to send a broadcast only received in the current application process. This broadcast is only used in the same process, which avoids the redundancy conflict received by multiple processes.

The Android system has provided some action value marked broadcasts, which will be sent to notify other applications after some common system operations. These system broadcasts are defined in the android.content.intent class in the form of static constants. For example, when the user modifies the flight mode status, the system will send intent.action_ AIRPLANE_ MODE_ The changed value is broadcast as an action; When the user lights up the screen, the system will send intent.action_ SCREEN_ On value is broadcast as action; When a new application is installed, the system will send intent.action_ PACKAGE_ The add value is broadcast as an action...

As the main component for receiving and processing broadcast messages, the broadcast receiver must inherit from android.content.broadcastreceiver class and implement the abstract method onReceive (context, content, intent) of this class. Receive the broadcast message in the onReceive () method, where the parameter context is the context of the current broadcast receiver, and the parameter intent is the content of the received broadcast message.

If the process of other applications calls back the onReceive () method of the broadcast receiver every time it sends a broadcast, it is a cumbersome process for each broadcast receiver. Therefore, the action parameter specified in the sent broadcast intent must specify a unique flag value. Only the broadcast receiver who has registered the action will call back the onReceive () method.

Before the target version of the application was Android 8.0, that is, the API was 26, the registration methods of broadcast recipients were static registration and dynamic registration. Since Android 8.0, except for some exceptional broadcast behaviors, only dynamic registration can be adopted.

The static registration method of broadcast receiver is similar to that of interface activity and service. Both of them need to declare the current component information in the < Application > < / Application > tag of the manifest file. The broadcast receiver declaration uses the < receiver > < / receiver > tag, under which the Android: name attribute is bound to the customized broadcast receiver. In the < receiver > < / receiver > tag, you can also use the < intent Filter > < / intent Filter > tag as the intention filter. Embed the < action Android: name = "" / > tag in the intention filtering tag to mark the unique action attribute value of the system bound by the current broadcast receiver. There can be multiple < action / > tags. As long as one of the sent action parameters corresponds to it, the onreceiver() method of the current broadcast receiver will be called.

For a statically registered broadcast receiver, the system traverses its registered broadcast action value after the application is installed, so that when other applications send the broadcast of the corresponding action, it matches it. If the match is consistent, the instantiation object of the broadcast receiver is created, and then the onreceiver () method of the broadcast receiver is called. After the execution of this method, The current broadcast recipient object is destroyed. When another application sends the corresponding broadcast again, it is also necessary to create the instantiated object of the new broadcast receiver again and call its onreceiver () method. This is the difference between broadcast receiver and other components.

For a dynamically registered broadcast receiver, broadcastreceiver can be registered in the interface activity or service where there is a context object. First, call the constructor of the custom broadcast receiver broadcastreceiver to create its instantiated object. Secondly, you need to create its instantiated object with android.content.intentfilter intention filter class. The action parameter can be set through a parameter construction method of the intentfilter class or the setaction (string action) method of the object. Where registration is required, call the registerreceiver (broadcastreceiver, intentfilter filter) series methods of the context object of the context environment to bind and register the created custom broadcast receiver broadcastreceiver with the intentfilter object with the action parameter set. After this logic is registered, the action broadcast sent by other processes can be received in the bound custom broadcast receiver and its onreceiver () method can be called. At the same level of the registered location, it is not necessary to process the received broadcast. It is necessary to deregister to reduce the system consumption. Call the unregisterreceiver (broadcastreceiver receiver) method of the context object of the context environment and pass in the previously registered broadcastreceiver object.

For dynamically registered broadcast recipients, they usually need to register and unregister in the life cycle method of component pairing, so their life cycle must be less than that of the registered component.

The broadcast mechanism provided by the Android system only needs to know the action attribute value of the broadcast to send or process the broadcast. This is not only between different processes, but also very convenient to use in the same process. However, officials suggest not to abuse the broadcast receiver, otherwise the system will slow down. In the general development process, it is often used to create the broadcast receiver of system broadcast, and assist the broadcast receiver of custom broadcast to be used between processes.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>