Android ActivityManagerService啟動流程詳解
概述
AMS是系統(tǒng)的引導服務,應用進程的啟動、切換和調度、四大組件的啟動和管理都需要AMS的支持。從這里可以看出AMS的功能會十分的繁多,當然它并不是一個類承擔這個重責,它有一些關聯(lián)類。
AMS的啟動流程
1:SystemServer#main -> 2:SystemServer#run -> 3:SystemServiceManager#startBootstrapServices
1:首先SystemServer進程運行main函數, main函數內部只調用了一個自己的run()方法.
public static void main(String[] args) { new SystemServer().run(); }
2:SystemServer的run()方法注釋非常多,可以自己看一下.中文注釋沒有使用//了, 不然顏色比較難以看清
private void run() { <--開始!--> // Prepare the main looper thread (this thread). <--準備主線程(該線程)--> android.os.Process.setThreadPriority( android.os.Process.THREAD_PRIORITY_FOREGROUND); android.os.Process.setCanSelfBackground(false); Looper.prepareMainLooper(); Looper.getMainLooper().setSlowLogThresholdMs( SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS); ? <--初始化native服務--> System.loadLibrary("android_servers"); <--初始化系統(tǒng)上下文--> createSystemContext(); <--創(chuàng)建system service manager!!!--> mSystemServiceManager = new SystemServiceManager(mSystemContext); mSystemServiceManager.setStartInfo(mRuntimeRestart, mRuntimeStartElapsedTime, mRuntimeStartUptime); LocalServices.addService(SystemServiceManager.class, mSystemServiceManager); // Prepare the thread pool for init tasks that can be parallelized SystemServerInitThreadPool.get(); ? ? <--打開系統(tǒng)服務--> <--啟動引導服務--> <--用SystemServiceManager啟動了ActivityManagerService、PowerManagerService、 PackageManagerService等服務--> startBootstrapServices(); <--核心服務--> <--啟動BatteryService、UsageStatsService和WebViewUpdateService--> startCoreServices(); <--啟動其他服務--> <--啟動了WMS,CameraService、AlarmManagerService、VrManagerService等服務--> startOtherServices(); <--Loop forever--> Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); }
3:SystemServer的startBootstrapServices()方法
SystemServer.java //啟動AMS private void startBootstrapServices() { ... <--調用SystemServiceManager的startSErvice()方法, 傳入Lifecycle.class字節(jié)碼文件的參數, 通過反射實例化Lifecycle對象,并啟動AMS(通過這個參數"ActivityManagerService.Lifecycle.class"可以看出 Lifecycle是AMS的一個內部類)--> mActivityManagerService = mSystemServiceManager.startService( ActivityManagerService.Lifecycle.class).getService(); ? mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); } ? SystemServiceManager.java public SystemService startService(String className) { <--使用反射獲取.class文件--> Class serviceClass = Class.forName(className); return this.startService(serviceClass); } SystemServiceManager.java public <T extends SystemService> T startService(Class<T> serviceClass) { String name = serviceClass.getName(); SystemService service; Constructor<T> constructor = serviceClass.getConstructor(Context.class); <--通過反射,調用constructor的newInstance方法來創(chuàng)建Lifecycle類型的service對象--> service = (SystemService)constructor.newInstance(this.mContext); <--把該服務加入到系統(tǒng)服務集合當中, 該系統(tǒng)服務集合就是SystemServiceManager類的list類型的成員變量--> this.mServices.add(service); <--調用反射類的onStart()方法開啟AMS服務(實際上Lifecycle內部類雖然是靜態(tài)的, 但是顯示的擁有一個AMS的對象, 該方法就是利用這個AMS對象調用AMS的start()方法)--> service.onStart(); <--返回該服務--> return service; } ?
19年的時候初入安卓系統(tǒng)開發(fā)的崗位,對整個系統(tǒng)的認識比較少。 現在看來AMS的啟動過程相對來說是比較簡單的。 在系統(tǒng)開機啟動之后,system_server會執(zhí)行三大服務啟動
startBootstrapServices() 啟動引導服務,在這里實際上已經new了AMS,在new方法里,已經初始化了AMS的大部分重要的屬性。AMS的Looper和各種handler也是在這里準備好的。 private void startBootstrapServices() { ... ActivityTaskManagerService atm = mSystemServiceManager.startService( ActivityTaskManagerService.Lifecycle.class).getService(); mActivityManagerService = ActivityManagerService.Lifecycle.startService( mSystemServiceManager, atm); mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); ... } public static final class Lifecycle extends SystemService { private final ActivityManagerService mService; private static ActivityTaskManagerService sAtm; public Lifecycle(Context var1) { super(var1); this.mService = new ActivityManagerService(var1, sAtm); } public static ActivityManagerService startService(SystemServiceManager var0, ActivityTaskManagerService var1) { sAtm = var1; return ((ActivityManagerService.Lifecycle)var0.startService(ActivityManagerService.Lifecycle.class)).getService(); }
在創(chuàng)建完AMS之后,system_server的run方法會執(zhí)行到startOtherServices(),在啟動“其他服務”完畢之后,會調入到AMS的systemReady()方法,在這里會啟動launcher。 可以說,在這個方法執(zhí)行完畢之后,系統(tǒng)就已經啟動完成了。如果我們先要在系統(tǒng)啟動的過程中在java framework中加入自己的代碼,可以在systemReady()這個方法中,完成。(比如注冊自己的廣播接受器)
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) { if (bootingSystemUser) { mAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady"); } }
啟動流程圖
到此這篇關于Android ActivityManagerService啟動流程詳解的文章就介紹到這了,更多相關Android ActivityManagerService內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
深入android中The connection to adb is
本篇文章是對android中The connection to adb is down的問題以及解決方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05淺談Android中關于靜態(tài)變量(static)的使用問題
本文主要介紹了Android中關于靜態(tài)變量(static)的使用問題,具有一定的參考作用,下面跟著小編一起來看下吧2017-01-01Android對話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解
這篇文章主要介紹了Android對話框中的提醒對話框AlertDialog、日期對話框DatePickerDialog、時間對話框TimePickerDialog使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-09-09Android4.X讀取SIM卡短信和聯(lián)系人相關類實例分析
這篇文章主要介紹了Android 4.X讀取SIM卡短信和聯(lián)系人相關類,以實例形式分析了Android 4.X讀取SIM卡短信和聯(lián)系人的兩個相關類的功能、用法與注意事項,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android Retrofit實現多圖片/文件、圖文上傳功能
Retrofit是Square開發(fā)的一個Android和Java的REST客戶端庫。這個庫非常簡單并且具有很多特性,相比其他的網絡庫,更容易讓初學者快速掌握2017-03-03