Android Source--broadcast Receiver

Source: Internet
Author: User

Android source code Analysis--Broadcast

Broadcasting is a way of delivering messages between components. The implementation of broadcasting mechanism is based on the binder interprocess communication.

Binder interprocess communication and broadcast differences: In binder, the client component and service component must first obtain a proxy object for the service before the client component has to know the presence of the service component beforehand. The broadcast sender is not aware of the presence of the broadcast receiver beforehand. Therefore, the coupling between the broadcast sender and the receiver is low .

Broadcasts are implemented based on the message Publishing and subscriber model.

In the same situation, a dynamically registered broadcast recipient receives the broadcast preferentially than a statically registered broadcast recipient.

Android.intent.action.SCREEN_OFF Android.intent.action.SCREEN_ON, these two types of broadcasts can only be processed by dynamically registered receivers

The sender specifies how the broadcast can be received by: Set custom permissions.

Registration process for broadcast receivers

Contextwrapper.registerreceiver

Contextimpl. Registerreceiver

Activitymanagerproxy.registerreceiver

Activitymanagerservice.registerreceiver

The broadcast sending process broadcasts the main process:
    1. The broadcast sender sends a broadcast to AMS.
    2. After AMS receives a broadcast, it first finds the receiver of the broadcast, then adds them to a broadcast dispatch queue, and finally sends a message of type broadcast_intent_msg to the thread message queue where AMS resides.
    3. When the BROADCAST_INTENT_MSG message in AMS's thread message queue is processed, AMS will find the broadcast receiver that needs to receive the broadcast from the broadcast dispatch queue and send the corresponding broadcast to the process where they are located.
    4. The broadcast recipient is running an application process that receives the broadcasts sent by AMS, encapsulates the broadcast into a message, and sends it to the main thread message queue. When the message is processed, the application process sends the broadcast to the appropriate broadcast recipient for processing.

The 1th step-the broadcast sender sends a broadcast to AMS:

Contextwrapper.sendbroadcast

Contextimpl.sendbroadcast

Activitymanagerproxy.broadcaseintent

Activitymanagerservice. Broadcaseintent

2nd Step-Find the target broadcast receiver

Activityinfo ai = Appglobals.getpackagemanager (). Getreceiverinfo (Intent.getcomponent,

Stock_pm_flags);

If (ai!=null) {

Receivers = new ArrayList ();

ResolveInfo ri = new ResolveInfo ();

Ri.activityinfo = AI;

Receivers.add (RI);

}

AMS forwards broadcasts to the target broadcast recipients through a message processing mechanism.

AMS encapsulates the broadcast (unordered or ordered) as a broadcast forwarding task, and adds it to an internal ordered broadcast dispatch queue.

Dynamically registered broadcast receivers, statically registered broadcast receivers, two queues are ranked in order of precedence from highest to lowest.

if (curr.getpriority () >= curt.priority) illustrates that if a dynamically registered target broadcast receiver and a statically registered broadcast recipient have the same priority, the recipient of the dynamic registration is in the front, that is, the broadcast is received first.

Activitymanagerservice.schedulebroadcastlocked sends a type of BROADCAST_INTENT_MSG message.

3rd Step-handling messages sent by AMS

Handler.handlemessage

Activitymanagerservice.processnextbroadcast

Activitymanagerservice.delivertoregisteredreceiverlocked

If the value of broadcastfilter.requiredpermission is not NULL, then it means that Activitymanagerservice needs to check the rights of the broadcast sender.

If the value of the member variable requiredpermission of the Broadcastrecord object is not NULL, the AMS needs to check the broadcast recipient's permissions.

These two permissions are called the AMS member function checkcomponentpermission to check the other side of the PID and UID compliance requirements.

Activitymanagerservice.performreceiverlocked

4th Step--Broadcast receiver processing MSG

Applicationthread.scheduleregisteredreceiver

Innerreceiver.performreceive

Receiverdispatcher.performreceive

Args.run

Broadcastreceiver.onreceive

Android Source--broadcast Receiver

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.