網站首頁 編程語言 正文
概述
AMS是系統的引導服務,應用進程的啟動、切換和調度、四大組件的啟動和管理都需要AMS的支持。從這里可以看出AMS的功能會十分的繁多,當然它并不是一個類承擔這個重責,它有一些關聯類。
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");
<--初始化系統上下文-->
createSystemContext();
<--創建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();
?
?
<--打開系統服務-->
<--啟動引導服務-->
<--用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字節碼文件的參數,
通過反射實例化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方法來創建Lifecycle類型的service對象-->
service = (SystemService)constructor.newInstance(this.mContext);
<--把該服務加入到系統服務集合當中, 該系統服務集合就是SystemServiceManager類的list類型的成員變量-->
this.mServices.add(service);
<--調用反射類的onStart()方法開啟AMS服務(實際上Lifecycle內部類雖然是靜態的,
但是顯示的擁有一個AMS的對象, 該方法就是利用這個AMS對象調用AMS的start()方法)-->
service.onStart();
<--返回該服務-->
return service;
}
?
19年的時候初入安卓系統開發的崗位,對整個系統的認識比較少。 現在看來AMS的啟動過程相對來說是比較簡單的。 在系統開機啟動之后,system_server會執行三大服務啟動
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();
}
在創建完AMS之后,system_server的run方法會執行到startOtherServices(),在啟動“其他服務”完畢之后,會調入到AMS的systemReady()方法,在這里會啟動launcher。 可以說,在這個方法執行完畢之后,系統就已經啟動完成了。如果我們先要在系統啟動的過程中在java framework中加入自己的代碼,可以在systemReady()這個方法中,完成。(比如注冊自己的廣播接受器)
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
if (bootingSystemUser) {
mAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady");
}
}
啟動流程圖
原文鏈接:https://blog.csdn.net/Androidxiaofei/article/details/128851644
相關推薦
- 2022-05-12 python遍歷文件夾內文件并檢索文件中的中文內容
- 2022-01-26 阿里云服務器端口請求失敗(在控制臺把端口添加到服務器的安全組)
- 2023-05-31 python常用函數random()函數詳解_python
- 2022-12-09 C++?opencv學習之圖像像素的邏輯操作_C 語言
- 2022-05-19 Python學習之異常處理的避坑指南_python
- 2022-06-02 Python利用zhdate模塊實現農歷日期處理_python
- 2022-08-15 spring5:創建spring項目并引入jar包
- 2022-08-25 Python面向對象的三大特性封裝、繼承、多態_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支