The process of registering, sending, and receiving broadcasts

The registration, sending and receiving of broadcasts are closely related to AMS.

Registration of broadcasts

Broadcast registration can be divided into static registration and dynamic registration. Static registration is completed by PackageManagerService during application installation. I mainly analyze dynamic broadcast registration.

ContextImpl requests AMS to register a broadcast

  • When we need to register a broadcast dynamically, we need to call the registerReceiver method of the Context, and then call the registerReceiver method of ContextImpl on the registerReceiver of ContextWrapper. Its registerReceiverInternal method is eventually called.

  • In ContextImpl’s registerReceiverInternal method, the first is similar to the service binding, The RD object of type IIntentReceiver is obtained via the getReceiverDispatcher method of the mPackageInfo object of type LoadedApk for broadcast cross-process communication. IActivityManager’s registerReceiver method is then called, and finally AMS’s registerReceiver method is called, passing in an RD object of type IIntentReceiver.

  • In AMS’s registerReceiver method, the getRecordForAppLocked method is called to get the information about the application process that called the registered broadcast. Based on the process information, the intent corresponding to all sticky broadcasts stored in AMS is retrieved. Then, all matching intents are found and stored in the allSticky list, and finally added to the broadcast queue for execution.

  • In addition, the AMS registerReceiver calls the HashMap type, which holds the list of broadcast receivers for all application processes, mRegisteredReceivers. Obtain the broadcast ReceiverList from the previous IIntentReceiver object and pass it to create the BroadcastFilter, which describes the registered broadcast receivers. Finally, add BroadcastFilter to mReceiverResolver of IntentResolver type, so that when AMS receives broadcast, it can directly find the corresponding broadcast receiver from mReceiverResolver, so as to register broadcast.


Transmission of a broadcast

Broadcasts can be sent in many types, including unordered broadcasts (regular broadcasts), ordered broadcasts, and sticky broadcasts.

Android Broadcast:

1. Normal (unordered) broadcast: use sendBroadcast to sendBroadcast. This broadcast can be passed to each processor in turn for processing.

2. Orderly broadcast: Use sendOrderedBroadcast to send broadcast. The order in which the broadcast is processed on the processor side is determined by the priority of the processor. The higher-priority processor will intercept the message first and delete the message.

Sticky messages: Use sendStickyBroadcast to send broadcast messages. After being sent, the sticky message always exists in the message container of the system, waiting for the corresponding processor to process it. If there is no processor to process the message temporarily, it always stays in the waiting state in the message container. If the Receiver of sticky broadcast is destroyed, the message data will be automatically received in the next reconstruction.

Note: regular broadcasts and sticky messages cannot be intercepted, while ordered broadcasts can be.

Here we take the simplest ordinary broadcast transmission as an example.

ContextImpl requests AMS to send a broadcast

  • When we want to send an unordered broadcast, we call ContextImpl’s sendBroadcast in ContextWrapper’s sendBroadcast. AMS’s broadcastIntent method is eventually called.

  • In AMS’s broadcastIntent method, the broadcastIntentLocked method is called after the broadcastIntentLocked method is validated.

  • We do a lot of things in AMS’s broadcastIntentLocked method, we do a bunch of things with broadcastQueueForIntent, we build a broadcast queue, and then we add a new BroadcastRecord object to the broadcast queue. Execute the scheduleBroadcastLocked method of broadcast queues at the same time.


Reception of broadcast

AMS goes to the BroadcastReceiver to receive broadcasts

  • Sent in BroadcastQueue scheduleBroadcastLocked method, type of BROADCAST_INTENT_MSG types of messages, and end in the message processing call its processNextBroadcastLocked method, And in which traverse stores the unordered list of broadcasting, and then call deliverToRegisteredReceiverLocked described these disorderly broadcast information sent to the corresponding radio receivers.

  • In BroadcastQueue deliverToRegisteredReceiverLocked method mainly check the permission of the radio sender and radio receiver, and ultimately will call its performReceiveLocked method, Then call ApplicationThread scheduleRegisteredReceiver method in its approach.

  • In ApplicationThread scheduleRegisteredReceiver method will be called IIntentReceiver object of type receiver performReceive method, The IIntentReceiver is the client that the Binder communicates with, the InnerReceiver’s local proxy, which calls the InnerReceiver’s performReceive method, The performReceive method of ReceiverDispatcher is eventually called.

  • In the performReceive method of ReceiverDispatcher, objects of type Args are constructed and eventually mActivityThread(H), The Runnable obtained by the getRunnable method of the Args object is sent to the thread’s message queue for execution. The onReceive method of receiver of type BroadcastReceiver is called in the Runnable method of the Args object, so that the registered BroadcastReceiver receives the broadcast and gets the intent.


contact

Wechat official account