Android framework ATMS啟動(dòng)流程
1 前言
ATMS 即 ActivityTaskManagerService,用于管理 Activity 及其容器(任務(wù)、堆棧、顯示等)。ATMS 在 Android 10 中才出現(xiàn),由原來(lái)的 AMS(ActivityManagerService)分離而來(lái),承擔(dān)了 AMS 的部分職責(zé)。因此,在 AMS初始化過(guò)程中(AMS啟動(dòng)流程),也伴隨著了 ATMS 的初始化。本文主要介紹 ATMS 的啟動(dòng)流程和初始化過(guò)程。
(1)ATMS 創(chuàng)建流程

- SystemServer:依次調(diào)用 main()、run()、startBootstrapServices(),再調(diào)用 SystemServiceManager 的 startService() 方法,并將 Lifecyle.class 傳入;
- SystemServiceManager :startService() 方法通過(guò)反射調(diào)用 Lifecyle 的構(gòu)造方法,生成 Lifecyle 對(duì)象;
- Lifecyle:構(gòu)造方法中調(diào)用 ATMS 的構(gòu)造方法創(chuàng)建 ATMS 對(duì)象,并通過(guò) getService() 方法返回 ATMS 對(duì)象。
(2)ATMS 初始化
如圖,ATMS 在初始化時(shí)創(chuàng)建了圖中藍(lán)色類的對(duì)象。

2 ATMS 啟動(dòng)流程
(1)main
/frameworks/base/services/java/com/android/server/SystemServer.java
public static void main(String[] args) {
new SystemServer().run();
}
(2)run
/frameworks/base/services/java/com/android/server/SystemServer.java
private void run() {
try {
...
// 創(chuàng)建Looper
Looper.prepareMainLooper();
// 加載libandroid_servers.so
System.loadLibrary("android_servers");
// 創(chuàng)建系統(tǒng)的 Context:ContextImpl.createSystemContext(new ActivityThread())
createSystemContext();
// 創(chuàng)建 SystemServiceManager
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
...
}
...
try {
//啟動(dòng)引導(dǎo)服務(wù),ActivityManagerService、ActivityTaskManagerService、PackageManagerService、PowerManagerService、DisplayManagerService 等
startBootstrapServices();
//啟動(dòng)核心服務(wù),BatteryService、UsageStatusService 等
startCoreServices();
//啟動(dòng)其他服務(wù),InputManagerService、WindowManagerService、CameraService、AlarmManagerService 等
startOtherServices();
...
}
...
// 開啟消息循環(huán)
Looper.loop();
}
(3)startBootstrapServices
/frameworks/base/services/java/com/android/server/SystemServer.java
private void startBootstrapServices() {
...
//啟動(dòng) ATMS
ActivityTaskManagerService atm = mSystemServiceManager.startService(ActivityTaskManagerService.Lifecycle.class).getService();
//啟動(dòng) AMS,并將 ATMS 注入
mActivityManagerService = ActivityManagerService.Lifecycle.startService(mSystemServiceManager, atm);
...
}
(4)startService
/frameworks/base/services/core/java/com/android/server/SystemServiceManager.java
public <T extends SystemService> T startService(Class<T> serviceClass) {
try {
final String name = serviceClass.getName();
...
final T service;
try { //通過(guò)反射調(diào)用 serviceClass 的構(gòu)造方法 創(chuàng)建 Lifecycle 對(duì)象
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
}
...
startService(service);
return service;
}
...
}
public void startService(SystemService service) {
mServices.add(service); //mServices: ArrayList<SystemService>
...
try {
service.onStart(); //調(diào)用 Lifecycle 的 onStart 方法
}
...
}
(5)ATMS.Lifecycle
/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.Lifecycle.java
public static final class Lifecycle extends SystemService {
private final ActivityTaskManagerService mService;
public Lifecycle(Context context) {//被 SystemServiceManager 的 startService() 方法調(diào)用
super(context);
mService = new ActivityTaskManagerService(context);
}
public void onStart() {
//添加 ATMS 服務(wù),方便跨進(jìn)程調(diào)用:ServiceManager.addService(Context.ACTIVITY_TASK_SERVICE, mService, false, DUMP_FLAG_PRIORITY_DEFAULT)
publishBinderService(Context.ACTIVITY_TASK_SERVICE, mService);
mService.start(); //調(diào)用 ATMS 的 start() 方法
}
...
public ActivityTaskManagerService getService() {
return mService;
}
}
注意:onStart() 方法中調(diào)用 ATMS 的 start() 方法初始化(下文還會(huì)介紹)。 已通過(guò) ServiceManager.addService() 將 Context.ACTIVITY_TASK_SERVICE 與 ATMS 綁定,因此在其他進(jìn)程中可以通過(guò)如下方式獲取 ATMS。
IBinder b = ServiceManager.getService(Context.ACTIVITY_TASK_SERVICE); IActivityTaskManager atm = IActivityTaskManager.Stub.asInterface(b);
3 ATMS 初始化
(1)ATMS 的構(gòu)造方法
/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
public ActivityTaskManagerService(Context context) {
mContext = context;
...
mSystemThread = ActivityThread.currentActivityThread();
mUiContext = mSystemThread.getSystemUiContext(); //ContextImpl.createSystemUiContext(getSystemContext())
mLifecycleManager = new ClientLifecycleManager();
mInternal = new LocalService(); //ActivityTaskManagerInternal 的子類
...
}
(2)start
start() 方法被 Lifecycle 的 onStart() 方法調(diào)用,onStart() 方法又被 SystemServiceManager 的 startService() 方法調(diào)用。
/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
private void start() {
LocalServices.addService(ActivityTaskManagerInternal.class, mInternal);
}
mInternal 屬于 LocalService 類(ActivityTaskManagerInternal 的子類),在 ATMS 的構(gòu)造方法中創(chuàng)建。
(3)initialize
在 AMS 的構(gòu)造方法中,調(diào)用了 ATMS 的 initialize() 方法進(jìn)一步初始化。
/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public ActivityManagerService(Context systemContext, ActivityTaskManagerService atm) {
...
mHandlerThread = new ServiceThread(TAG, THREAD_PRIORITY_FOREGROUND, false);
...
mUserController = new UserController(this);
mPendingIntentController = new PendingIntentController(mHandlerThread.getLooper(), mUserController);
...
mIntentFirewall = new IntentFirewall(new IntentFirewallInterface(), mHandler);
mActivityTaskManager = atm;
//進(jìn)一步初始化 ATMS
mActivityTaskManager.initialize(mIntentFirewall, mPendingIntentController, DisplayThread.get().getLooper());
...
}
/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
public void initialize(IntentFirewall intentFirewall, PendingIntentController intentController, Looper looper) {
mH = new H(looper);
mUiHandler = new UiHandler();
mIntentFirewall = intentFirewall;
...
mPendingIntentController = intentController;
mTempConfig.setToDefaults(); //定義時(shí)即被創(chuàng)建:mTempConfig = new Configuration()
...
//new ActivityStackSupervisor(this, mH.getLooper())
mStackSupervisor = createStackSupervisor();
mRootActivityContainer = new RootActivityContainer(this);
mRootActivityContainer.onConfigurationChanged(mTempConfig);
...
mLockTaskController = new LockTaskController(mContext, mStackSupervisor, mH);
mActivityStartController = new ActivityStartController(this);
mRecentTasks = createRecentTasks(); //new RecentTasks(this, mStackSupervisor)
mStackSupervisor.setRecentTasks(mRecentTasks);
...
}
(4)onActivityManagerInternalAdded
在 AMS 的 start() 方法中,調(diào)用了 ATMS 的 onActivityManagerInternalAdded() 方法進(jìn)一步初始化。
/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
private void start() {
...
LocalServices.addService(ActivityManagerInternal.class, new LocalService());
//調(diào)用 ATMS 的 onActivityManagerInternalAdded 方法進(jìn)一步初始化
mActivityTaskManager.onActivityManagerInternalAdded();
mUgmInternal.onActivityManagerInternalAdded();
mPendingIntentController.onActivityManagerInternalAdded();
...
}
/frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
public void onActivityManagerInternalAdded() {
synchronized (mGlobalLock) {
mAmInternal = LocalServices.getService(ActivityManagerInternal.class);
mUgmInternal = LocalServices.getService(UriGrantsManagerInternal.class);
}
}
(5)ActivityStartController
ActivityStartController 作為 ATMS 的一個(gè)重要成員,控制了 Activity 的啟動(dòng),因此我們繼續(xù)跟進(jìn) ActivityStartController 的構(gòu)造方法。
/frameworks/base/services/core/java/com/android/server/am/ActivityStartController.java
ActivityStartController(ActivityTaskManagerService service) {
this(service, service.mStackSupervisor,
new DefaultFactory(service, service.mStackSupervisor,
new ActivityStartInterceptor(service, service.mStackSupervisor)));
}
ActivityStartController(ActivityTaskManagerService service, ActivityStackSupervisor supervisor,
Factory factory) {
mService = service;
mSupervisor = supervisor;
mHandler = new StartHandler(mService.mH.getLooper());
mFactory = factory;
mFactory.setController(this);
...
}
(6)DefaultFactory
DefaultFactory 是 ActivityStarter 的靜態(tài)內(nèi)部類,負(fù)責(zé) ActivityStarter 的創(chuàng)建和回收。因此我們繼續(xù)跟進(jìn) DefaultFactory 類。
/frameworks/base/services/core/java/com/android/server/wm/ActivityStarter.DefaultFactory.java
static class DefaultFactory implements Factory {
...
private ActivityStartController mController;
private ActivityTaskManagerService mService;
private ActivityStackSupervisor mSupervisor;
private ActivityStartInterceptor mInterceptor;
//MAX_STARTER_COUNT = 3
private SynchronizedPool<ActivityStarter> mStarterPool = new SynchronizedPool<>(MAX_STARTER_COUNT);
DefaultFactory(ActivityTaskManagerService service, ActivityStackSupervisor supervisor, ActivityStartInterceptor interceptor) {
mService = service;
mSupervisor = supervisor;
mInterceptor = interceptor;
}
public ActivityStarter obtain() {
ActivityStarter starter = mStarterPool.acquire();
if (starter == null) {
starter = new ActivityStarter(mController, mService, mSupervisor, mInterceptor);
}
return starter;
}
@Override
public void recycle(ActivityStarter starter) {
mStarterPool.release(starter);
}
}以上就是Android framework ATMS啟動(dòng)流程的詳細(xì)內(nèi)容,更多關(guān)于Android framework ATMS啟動(dòng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android實(shí)現(xiàn)系統(tǒng)日歷同步日程
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)系統(tǒng)日歷同步日程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
基于Android實(shí)現(xiàn)隨手指移動(dòng)的ImageView
這篇文章主要介紹了基于Android實(shí)現(xiàn)隨手指移動(dòng)的ImageView的相關(guān)資料,需要的朋友可以參考下2016-01-01
淺談RecyclerView(完美替代ListView,GridView)
RecyclerView絕對(duì)是一款功能強(qiáng)大的控件,涵蓋了ListView,GridView,瀑布流等數(shù)據(jù)表現(xiàn)的形式。本文對(duì)其進(jìn)行系統(tǒng)介紹,有需要的朋友可以看下2016-12-12
導(dǎo)入takephoto庫(kù)編譯失敗與glide庫(kù)沖突應(yīng)排除依賴
今天小編就為大家分享一篇關(guān)于導(dǎo)入takephoto庫(kù)編譯失敗與glide庫(kù)沖突應(yīng)排除依賴的文章,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
Android 校驗(yàn)email是否合法實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 校驗(yàn)email是否合法實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Flutter?Widget開發(fā)之Focus組件圖文詳解
這篇文章主要為大家介紹了Flutter?Widget開發(fā)之Focus組件圖文詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
解決VSCode調(diào)試react-native android項(xiàng)目錯(cuò)誤問(wèn)題
這篇文章主要介紹了VSCode調(diào)試react-native android項(xiàng)目錯(cuò)誤解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Android中WebView常見問(wèn)題及解決方案匯總
本篇文章主要介紹了Android中WebView常見問(wèn)題及解決方案匯總,把WebView遇到的問(wèn)題詳細(xì)的羅列下來(lái),有需要的朋友可以了解一下。2016-11-11
Android開發(fā)基于ViewPager+GridView實(shí)現(xiàn)仿大眾點(diǎn)評(píng)橫向滑動(dòng)功能
這篇文章主要介紹了Android開發(fā)基于ViewPager+GridView實(shí)現(xiàn)仿大眾點(diǎn)評(píng)橫向滑動(dòng)功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09

