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

Android ActivityManagerService啟動(dòng)流程詳解

 更新時(shí)間:2023年02月17日 09:24:23   作者:守住Android最后的光  
這篇文章主要介紹了Android ActivityManagerService啟動(dòng)流程,AMS,即ActivityManagerService,是安卓java framework的一個(gè)服務(wù),運(yùn)行在system_server進(jìn)程。此服務(wù)十分重要,因?yàn)樗芾碇沧康乃拇蠼M件,是安卓APP開(kāi)發(fā)者最常接觸到的一個(gè)服務(wù)

概述

AMS是系統(tǒng)的引導(dǎo)服務(wù),應(yīng)用進(jìn)程的啟動(dòng)、切換和調(diào)度、四大組件的啟動(dòng)和管理都需要AMS的支持。從這里可以看出AMS的功能會(huì)十分的繁多,當(dāng)然它并不是一個(gè)類(lèi)承擔(dān)這個(gè)重責(zé),它有一些關(guān)聯(lián)類(lèi)。

AMS的啟動(dòng)流程

1:SystemServer#main -> 2:SystemServer#run -> 3:SystemServiceManager#startBootstrapServices

1:首先SystemServer進(jìn)程運(yùn)行main函數(shù), main函數(shù)內(nèi)部只調(diào)用了一個(gè)自己的run()方法.

 public static void main(String[] args) {
        new SystemServer().run();
    }

2:SystemServer的run()方法注釋非常多,可以自己看一下.中文注釋沒(méi)有使用//了, 不然顏色比較難以看清

 private void run() {
       <--開(kāi)始!-->
            // Prepare the main looper thread (this thread).
            <--準(zhǔn)備主線程(該線程)-->
            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();
?
?
       <--打開(kāi)系統(tǒng)服務(wù)-->
            <--啟動(dòng)引導(dǎo)服務(wù)-->
            <--用SystemServiceManager啟動(dòng)了ActivityManagerService、PowerManagerService、 PackageManagerService等服務(wù)-->
            startBootstrapServices();
            <--核心服務(wù)-->
            <--啟動(dòng)BatteryService、UsageStatsService和WebViewUpdateService-->
            startCoreServices();
            <--啟動(dòng)其他服務(wù)-->
            <--啟動(dòng)了WMS,CameraService、AlarmManagerService、VrManagerService等服務(wù)-->
            startOtherServices();
      <--Loop forever-->
        Looper.loop();
        throw new RuntimeException("Main thread loop unexpectedly exited");
    }

3:SystemServer的startBootstrapServices()方法

SystemServer.java
//啟動(dòng)AMS
 private void startBootstrapServices() {
        ...
        <--調(diào)用SystemServiceManager的startSErvice()方法, 傳入Lifecycle.class字節(jié)碼文件的參數(shù), 
        通過(guò)反射實(shí)例化Lifecycle對(duì)象,并啟動(dòng)AMS(通過(guò)這個(gè)參數(shù)"ActivityManagerService.Lifecycle.class"可以看出
        Lifecycle是AMS的一個(gè)內(nèi)部類(lè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);
        <--通過(guò)反射,調(diào)用constructor的newInstance方法來(lái)創(chuàng)建Lifecycle類(lèi)型的service對(duì)象-->
        service = (SystemService)constructor.newInstance(this.mContext);
        <--把該服務(wù)加入到系統(tǒng)服務(wù)集合當(dāng)中, 該系統(tǒng)服務(wù)集合就是SystemServiceManager類(lèi)的list類(lèi)型的成員變量-->
        this.mServices.add(service);
        <--調(diào)用反射類(lèi)的onStart()方法開(kāi)啟AMS服務(wù)(實(shí)際上Lifecycle內(nèi)部類(lèi)雖然是靜態(tài)的, 
        但是顯示的擁有一個(gè)AMS的對(duì)象, 該方法就是利用這個(gè)AMS對(duì)象調(diào)用AMS的start()方法)-->
        service.onStart();
        <--返回該服務(wù)-->
        return service; 
  }
?

19年的時(shí)候初入安卓系統(tǒng)開(kāi)發(fā)的崗位,對(duì)整個(gè)系統(tǒng)的認(rèn)識(shí)比較少。 現(xiàn)在看來(lái)AMS的啟動(dòng)過(guò)程相對(duì)來(lái)說(shuō)是比較簡(jiǎn)單的。 在系統(tǒng)開(kāi)機(jī)啟動(dòng)之后,system_server會(huì)執(zhí)行三大服務(wù)啟動(dòng)

startBootstrapServices() 啟動(dòng)引導(dǎo)服務(wù),在這里實(shí)際上已經(jīng)new了AMS,在new方法里,已經(jīng)初始化了AMS的大部分重要的屬性。AMS的Looper和各種handler也是在這里準(zhǔn)備好的。
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方法會(huì)執(zhí)行到startOtherServices(),在啟動(dòng)“其他服務(wù)”完畢之后,會(huì)調(diào)入到AMS的systemReady()方法,在這里會(huì)啟動(dòng)launcher。 可以說(shuō),在這個(gè)方法執(zhí)行完畢之后,系統(tǒng)就已經(jīng)啟動(dòng)完成了。如果我們先要在系統(tǒng)啟動(dòng)的過(guò)程中在java framework中加入自己的代碼,可以在systemReady()這個(gè)方法中,完成。(比如注冊(cè)自己的廣播接受器)

public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
            if (bootingSystemUser) {
                mAtmInternal.startHomeOnAllDisplays(currentUserId, "systemReady");
            }
 }           

啟動(dòng)流程圖

到此這篇關(guān)于Android ActivityManagerService啟動(dòng)流程詳解的文章就介紹到這了,更多相關(guān)Android ActivityManagerService內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論