Broadcast相关的数据结构
1. Broadcast数据结构
broadcast.png2. BroadcastRecord
BroadcastRecord在AMS中表示一个广播的信息
ProcessRecord callerApp : 发送广播进程的信息
boolean ordered :是否是有序广播
boolean sticky:是否是粘性广播
List receivers:广播接收器,包括动态注册(BroadcastFilter)和静态注册(ResolveInfo)
IBinder receiver:Binder代理段,对应应用进程中的IIntentReceiver
3. LoadedApk
在应用进程注册BroadcastReceiver的时候,会将BroadcastReceiver保存到LoadedApk的mReceivers中,同时为当前的BroadcastRecevier生成一个对应的ReceiverDispatcher, ReceiverDispatcher中有一个IIntentReceiver对象,是Binder的服务端,然后将这个Binder类型传递给了AMS,AMS持有代理端,也就是BroadcastRecord中的Binder receiver对象.
BroadcastRecevier -- ReceiverDispatcher -- IIntentReceiver -- BroadcastRecord是一一对应的。
4. AMS的mStickyBroadcasts
/**
* State of all active sticky broadcasts per user. Keys are the action of the
* sticky Intent, values are an ArrayList of all broadcasted intents with
* that action (which should usually be one). The SparseArray is keyed
* by the user ID the sticky is for, and can include UserHandle.USER_ALL
* for stickies that are sent to all users.
*/
final SparseArray<ArrayMap<String, ArrayList<Intent>>> mStickyBroadcasts =
new SparseArray<ArrayMap<String, ArrayList<Intent>>>();
mStickyBroadcasts 首先是以userID为key,保存了每个用户下的粘性广播 ArrayMap<String, ArrayList<Intent>>
而每个用户下的粘性广播又是以String类型的Action为key,保存对应的intent的列表. 同一个Action可能会发多次广播. 每次广播是一个intent对象
mStickyBroadcasts 用来保存系统中发送的所有的sticky broadcast, 每当系统中发送一次sticky broadcast, 都会保存到该容器中.
当有进程广播注册的时候,如果注册了对应类型的Action广播,会立刻收到对应的粘性广播
5. AMS的mRegisteredReceivers
/**
* Keeps track of all IIntentReceivers that have been registered for broadcasts.
* Hash keys are the receiver IBinder, hash value is a ReceiverList.
*/
final HashMap<IBinder, ReceiverList> mRegisteredReceivers = new HashMap<>();
保存了系统中所有的动态注册的广播,以应用进程中的IIntentReceiver的binder代理端为key, 保存了该Receiver对应的所有IntentFilter,是一个一对多的关系,一个BroadcastReceiver对应多个intentFilter。
ReceiverList继承了ArrayList<BroadcastFilter>, 保存一个BroadcastReceiver对应的IIntentReceiver以及多个BroadcastFilter,一个BroadcastFilter对应一个IntentFilter。
对应关系.png每次注册都会向对应IIntentReceiver所在的ReceiverList列表中添加一个BroadcastFilter
只有registerReceiver()过程才会创建BroadcastFilter,也就是该对象用于动态注册的广播Receiver;
BroadcastFilter继承于IntentFilter.同样地, 只有registerReceiver()过程才会创建ReceiverList;
6. ProcessRecord的receivers
// all IIntentReceivers that are registered from this process.
final ArraySet<ReceiverList> receivers = new ArraySet<>();
receivers中保存了当前进程所有动态注册的广播接收者
7. BroadcastQueue
ArrayList<BroadcastRecord> mParallelBroadcasts = new ArrayList<>()
保存了将要发送的 非有序 动态注册的广播
ArrayList<BroadcastRecord> mOrderedBroadcasts = new ArrayList<>()
保存了将要发送的静态注册的广播和有序广播