Android ActivityManagerService啟動流程詳解
概述
AMS是系統(tǒng)的引導(dǎo)服務(wù),應(yīng)用進程的啟動、切換和調(diào)度、四大組件的啟動和管理都需要AMS的支持。從這里可以看出AMS的功能會十分的繁多,當然它并不是一個類承擔這個重責(zé),它有一些關(guān)聯(lián)類。
AMS的啟動流程
1:SystemServer#main -> 2:SystemServer#run -> 3:SystemServiceManager#startBootstrapServices
1:首先SystemServer進程運行main函數(shù), main函數(shù)內(nèi)部只調(diào)用了一個自己的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服務(wù)-->
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)服務(wù)-->
<--啟動引導(dǎo)服務(wù)-->
<--用SystemServiceManager啟動了ActivityManagerService、PowerManagerService、 PackageManagerService等服務(wù)-->
startBootstrapServices();
<--核心服務(wù)-->
<--啟動BatteryService、UsageStatsService和WebViewUpdateService-->
startCoreServices();
<--啟動其他服務(wù)-->
<--啟動了WMS,CameraService、AlarmManagerService、VrManagerService等服務(wù)-->
startOtherServices();
<--Loop forever-->
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}3:SystemServer的startBootstrapServices()方法
SystemServer.java
//啟動AMS
private void startBootstrapServices() {
...
<--調(diào)用SystemServiceManager的startSErvice()方法, 傳入Lifecycle.class字節(jié)碼文件的參數(shù),
通過反射實例化Lifecycle對象,并啟動AMS(通過這個參數(shù)"ActivityManagerService.Lifecycle.class"可以看出
Lifecycle是AMS的一個內(nèi)部類)-->
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);
<--通過反射,調(diào)用constructor的newInstance方法來創(chuàng)建Lifecycle類型的service對象-->
service = (SystemService)constructor.newInstance(this.mContext);
<--把該服務(wù)加入到系統(tǒng)服務(wù)集合當中, 該系統(tǒng)服務(wù)集合就是SystemServiceManager類的list類型的成員變量-->
this.mServices.add(service);
<--調(diào)用反射類的onStart()方法開啟AMS服務(wù)(實際上Lifecycle內(nèi)部類雖然是靜態(tài)的,
但是顯示的擁有一個AMS的對象, 該方法就是利用這個AMS對象調(diào)用AMS的start()方法)-->
service.onStart();
<--返回該服務(wù)-->
return service;
}
?19年的時候初入安卓系統(tǒng)開發(fā)的崗位,對整個系統(tǒng)的認識比較少。 現(xiàn)在看來AMS的啟動過程相對來說是比較簡單的。 在系統(tǒng)開機啟動之后,system_server會執(zhí)行三大服務(wù)啟動
startBootstrapServices() 啟動引導(dǎo)服務(wù),在這里實際上已經(jīng)new了AMS,在new方法里,已經(jīng)初始化了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(),在啟動“其他服務(wù)”完畢之后,會調(diào)入到AMS的systemReady()方法,在這里會啟動launcher。 可以說,在這個方法執(zhí)行完畢之后,系統(tǒng)就已經(jīng)啟動完成了。如果我們先要在系統(tǒng)啟動的過程中在java framework中加入自己的代碼,可以在systemReady()這個方法中,完成。(比如注冊自己的廣播接受器)
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
if (bootingSystemUser) {
mAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady");
}
}
啟動流程圖

到此這篇關(guān)于Android ActivityManagerService啟動流程詳解的文章就介紹到這了,更多相關(guān)Android ActivityManagerService內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
調(diào)用startService會拋出IllegalStateException異常解決
這篇文章主要為大家介紹了調(diào)用startService會拋出IllegalStateException異常解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
Android studio實現(xiàn)簡易計算器App功能
這篇文章主要為大家詳細介紹了Android studio實現(xiàn)簡易計算器App功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
Android直播軟件搭建之實現(xiàn)背景顏色滑動漸變效果的詳細代碼
這篇文章主要介紹了Android直播軟件搭建之實現(xiàn)背景顏色滑動漸變效果的詳細代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09

