AMS主要是負責四大組件的啟動,進程調度以及在AMS中還會啟動部分系統服務,主要是其他服務:startOtherServices
1:AMS的數據結構
2:ActivityStackSuperVisor
ActivityStackSuperVisor是在ActivityManagerService初始化的時候創建的,ActivityStackSuperVisor主要是用來管理ActivityStack的,其內部管理了三個ActivityStack:mHomeStack,mFocusedStack,mLastFocusedStack,其中mHomeStack主要是用來管理LauncherActivity相關的Stack,mFocusedStack是指當前的焦點棧,mLastFocusedStack是上一次處于前臺的棧,并且還有一個很重要的成員mActivityDisplays,在android中一塊顯示屏幕對應一個ActivityDisplay,可以通過ActivityDisplay來管理某一顯示屏上的Activity
3:ActivityDiplay
ActivityDisplay可以理解成一塊屏幕,不同的屏幕通過displayId來區分,其管理者一組ActivityStack,當然也管理著特殊的棧
private ActivityStack mHomeStack = null;
private ActivityStack mRecentsStack = null;
private ActivityStack mPinnedStack = null;
private ActivityStack mSplitScreenPrimaryStack = null;
4:ActivityStack
ActivityStack才是我們通常說的Activity棧,其維護著一組TaskRecord和當前處理running狀態的ActivityRecord,當然其也維護這幾個比較特殊的Activity
/**
* The back history of all previous (and possibly still
* running) activities. It contains #TaskRecord objects.
*/
private final ArrayList<TaskRecord> mTaskHistory = new ArrayList<>();
/**
* List of running activities, sorted by recent usage.
* The first entry in the list is the least recently used.
* It contains HistoryRecord objects.
*/
final ArrayList<ActivityRecord> mLRUActivities = new ArrayList<>
/**
* When we are in the process of pausing an activity, before starting the
* next one, this variable holds the activity that is currently being paused.
*/
ActivityRecord mPausingActivity = null;
/**
* This is the last activity that we put into the paused state. This is
* used to determine if we need to do an activity transition while sleeping,
* when we normally hold the top activity paused.
*/
ActivityRecord mLastPausedActivity = null;
/**
* Activities that specify No History must be removed once the user navigates away from them.
* If the device goes to sleep with such an activity in the paused state then we save it here
* and finish it later if another activity replaces it on wakeup.
*/
ActivityRecord mLastNoHistoryActivity = null;
/**
* Current activity that is resumed, or null if there is none.
*/
ActivityRecord mResumedActivity = null;
ActivityStack對應WindowManagerService的數據結構是TaskStack
5:TaskRecord
TaskRecord是用來管理一組相同taskId的ActivityRecord,其內部有一個很重要的數組:ArrayList<ActivityRecord> mActivities
final int taskId; // Unique identifier for this task.
String affinity;
/** List of all activities in the task arranged in history order */
final ArrayList<ActivityRecord> mActivities;
/** Current stack. Setter must always be used to update the value. */
private ActivityStack mStack;
6:ActivityRecord
ActivityRecord是Activity在AMS中的描述,代表一個真正的Activity,與應用進程中的Activity是一一對應的,應用程序,AMS,WMS可以通過appToken關聯起來,appToken是在ActivityRecord的構造方法中初始化的
ActivityRecord(ActivityManagerService _service, ProcessRecord _caller, int _launchedFromPid,
int _launchedFromUid, String _launchedFromPackage, Intent _intent, String _resolvedType,
ActivityInfo aInfo, Configuration _configuration,
ActivityRecord _resultTo, String _resultWho, int _reqCode,
boolean _componentSpecified, boolean _rootVoiceInteraction,
ActivityStackSupervisor supervisor, ActivityOptions options,
ActivityRecord sourceRecord) {
service = _service;
appToken = new Token(this, _intent);
ActivityRecord中成員變量
private TaskRecord task; // the task this is in.
final IApplicationToken.Stub appToken; // window manager token
AppWindowContainerController mWindowContainerController;
final ActivityInfo info; // all about me
通過task可以找到其對應的task,appToken是其的標識,mWindowContainerController是和WMS的AppWindowToken關聯的,info存儲的是Activity的所有信息