首页 > 代码库 > ActivityManagerService笔记

ActivityManagerService笔记

1、mIntentSenderRecords

是一个PendingIntentRecord哈希表。一个PendingIntentRecord对象对应的是应用层的一个PendingIntent对象(延迟性的intent),其中包含send系列方法,主要用于触发PendingIntent的Intent行为。上层应用每获取一个PendingIntent对象时在AMS这边便会生成对应的PendingIntentRecord对象,并保存在mIntentSenderRecords哈希列表中,当上层应用调用PendingIntent.send()时,最终会调用到PendingIntentRecord.sendInner()函数,这个函数会将之前上层应用传下来的Intent进行触发(起Activity、发广播、起service)。PendingIntent对象是可以传递给其他应用的,这个应该才是设计初衷?

2、mRegisteredReceivers

是一个ReceiverList哈希表。ReceiverList对应一个广播接收器,mRegisteredReceivers保存着系统中的所有广播接收器。ReceiverList继承于ArrayList<BroadcastFilter>,实质上就是一个BroadcastFilter列表,源码是这样解释ReceiverList的:

/**
 * A receiver object that has registered for one or more broadcasts.
 * The ArrayList holds BroadcastFilter objects.
 */
熟悉广播接收器的都知道,一个广播接收器可以有多个IntentFilter过滤器,每个过滤器可以通过addAction添加多个action。

public Intent registerReceiver(IApplicationThread caller, String callerPackage,
            IIntentReceiver receiver, IntentFilter filter, String permission, int userId) {
            ...........
            ReceiverList rl = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
            if (rl == null) {
                rl = new ReceiverList(this, callerApp, callingPid, callingUid,
                        userId, receiver);
                if (rl.app != null) {
                    rl.app.receivers.add(rl);
                } else {
                    try {
                        receiver.asBinder().linkToDeath(rl, 0);
                    } catch (RemoteException e) {
                        return sticky;
                    }
                    rl.linkedToDeath = true;
                }
                mRegisteredReceivers.put(receiver.asBinder(), rl);
            }
           .............
           BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage,
                    permission, callingUid, userId);
            rl.add(bf);
            mReceiverResolver.addFilter(bf);
            if (allSticky != null) {
                ArrayList receivers = new ArrayList();
                receivers.add(bf);
                int N = allSticky.size();
                for (int i=0; i<N; i++) {
                    Intent intent = (Intent)allSticky.get(i);
                    BroadcastQueue queue = broadcastQueueForIntent(intent);
                    BroadcastRecord r = new BroadcastRecord(queue, intent, null,
                            null, -1, -1, null, null, AppOpsManager.OP_NONE, receivers, null, 0,
                            null, null, false, true, true, -1);
                    queue.enqueueParallelBroadcastLocked(r);
                    queue.scheduleBroadcastsLocked();
                }
            }
            return sticky;
        }
}
上面的代码可以看出一个广播接收器在AMS这边对应有一个ReceiverList,其实是一个BroadcastFilter列表,属于这个广播接受器的IntentFilter对应的BroadcastFilter对象保存在ReceiverList列表中。IntentFilter中的每个action,都会生成一个Intent,每个Intent又会对应new一个BroadcastRecord,然后根据这个Intent是否设置Intent.FLAG_RECEIVER_FOREGROUND来选择一个BroadcastQueue队列来将这个BroadcastRecord加入到队列中。有两个BroadcastQueue队列,分别是mFgBroadcastQueue和mBgBroadcastQueue。

ActivityManagerService笔记