Joke Collection Website - Public benefit messages - 22 broadcast mechanism
22 broadcast mechanism
Broadcast receivers are divided into two categories:
From the broadcast mode of transmission can be divided into three categories:
Broadcasting is recorded in the system as a BroadcastRecord object, which has several time-related member variables.
Broadcast registration is used for application development, and the method of registerReceiver () in Activity/Service is often called. Both Activity and Service are indirectly inherited from the Context abstract class, and the real work is handed over to the ContextImpl class. In addition, call getOuterContext () to get the outermost caller activity or service.
【 ContextImpl.java】
Among them, broadcastPermission has the permission control of broadcasting, and scheduler is used to specify on the recursive execution thread when receiving broadcasting. When scheduler=null, it means that it is executed in the main thread by default, which is also the most common usage.
【 ContextImpl.java】
Activity management. GetDefault () returns an ActivityManagerProxy object, abbreviated as AMP.
The parameter in this method is mmainthread. ApplicationThread (), which is the Bn end of Binder, is used for communication between the system_server process and this process.
【-》; LoadedApk.java】
Let ArrayMap and LoadedApk with BroadcastReceiver as keys. ReceiverDispatcher is marked as a value. Here mReceivers is an array mapping, with the context as the key and A as the value. For ReceiverDispatcher, create one when it doesn't exist.
Here mActivityThread is the handler of the current main thread that was previously passed.
ReceiverDispatcher has an internal class InnerReceiver, which inherits from IIntentReceiver.Stub. Obviously, this is a binding server, and the broadcast distributor can obtain the binding server object InnerReceiver through rd.getIIntentReceiver () for binding IPC communication.
【-》; ActivityManagerNative.java】
There are two Binder server objects, caller and receiver, which represent the process of performing the registration broadcast operation. AMP sends this information to AMS object in system_server process through Binder driver, and then enters AMS.registerReceiver.
【-》; ActivityManagerService.java】
Among them, mRegisteredReceivers records all registered broadcasts, with receiver IBinder as the key, Receiver List as the value and HashMap.
In the broadcast queue, there are two broadcast queues mparallelbroadcasts and morderedroadcasts, both of which have the data type of ArrayList.
The mLruProcesses data type is ArrayList.
This method is used to match whether the started intention data is matched successfully. There are four matches: action, type, data and category. Any unsuccessful match will fail.
Broadcastqueueforintent (intentintent) determines whether it is foreground or background broadcast by judging whether intent.getFlags () contains FLAG_RECEIVER_FOREGROUND, and then returns the corresponding broadcast queue mFgBroadcastQueue or mBgBroadcastQueue.
Register for broadcast:
In addition, when you register Sticky Radio:
After broadcast registration, another operation is in the process of broadcast transmission.
Sending a broadcast is to call the sendBroadcast () method in an activity or service. These methods are indirectly inherited from the Context abstract class, and the real work is given to the ContextImpl class.
【 ContextImpl.java】
【-》; ActivityManagerNative.java】
【-》; ActivityManagerService.java】
The broadcastIntent () method has two Boolean parameters serialized and sticky, which are used to jointly decide whether to broadcast normally, orderly or sticky. These parameters are as follows:
BroadcastIntentLocked method is relatively long and is divided into eight parts.
The most important work in this process is:
BroadcastReceiver has other signs, which are located in Intent.java constant:
Main functions:
This is mainly in the system-related 10 broadcast, so I will explain it here.
This process is mainly to add sticky broadcasts to the list and put them in mStickyBroadcasts.
Other considerations:
AMS.collectReceiverComponents:
There is a member variable mParallelBroadcasts in the broadcast queue, of type ArrayList.
The dynamically registered receivers are all merged into the receivers and processed in serial mode.
There is a member variable of type ArrayList in the broadcast queue.
Send broadcast process:
Treatment method:
It can be seen that no matter which broadcast mode it is, it is judged whether it is a foreground queue or a background queue according to the sign of intention through broadcastQueueForIntent (), and then the scheduleBroadcastsLocked method corresponding to the broadcast queue is called to process the broadcast;
In the process of sending a broadcast, execute the scheduleBroadcastsLocked method to handle the related broadcast.
【-》; BroadcastQueue.java】
When creating a BroadcastQueue object, mhandler = newBroadcastHandler (handler. get looper()); Then mHandler's handleMessage will handle it here:
You can see that BroadcastHandler uses the Looper of the "ActivityManager" thread.
【-》; BroadcastQueue.java】
The MS mService here is AMS, and the whole process is still relatively long, and it has always held AMS locks. Therefore, in the case of low broadcasting efficiency, the performance and fluency of this mobile phone will be seriously affected. Here, we should consider refining the granularity of synchronization locks.
- Previous article:What is a letter from Tik Tok?
- Next article:How does Huawei's mobile phone completely reject SMS?
- Related articles
- Where is Zhengzhou AIDS health certificate?
- How to open yellow, green, red, black, purple, pink, blue and brick by SMS?
- Where can I renew my forklift license?
- Greetings to relatives
- When the mobile phone comes in to send a text message, it doesn't display any new information, and there is no sound in standby state. You have to go to the inbox yourself.
- How to take a bus from Yulin West Street to Yipin Tianxia Street North is the fastest need.
- Daily greetings
- Unable to receive JD.COM SMS verification code.
- China greets classical Chinese in the New Year.
- Accept the blessing of the god of wealth