欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android ActivityManagerService啟動流程詳解

 更新時間:2023年02月17日 09:24:23   作者:守住Android最后的光  
這篇文章主要介紹了Android ActivityManagerService啟動流程,AMS,即ActivityManagerService,是安卓java framework的一個服務,運行在system_server進程。此服務十分重要,因為它管理著安卓的四大組件,是安卓APP開發(fā)者最常接觸到的一個服務

概述

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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論