系統(tǒng)服務(wù)解析:ActivityManagerService

ActivityManagerService 啟動流程

它是在SyetemServer進程中啟動的:
文件所在目錄: frameworks/base/services/java/com/android/server/SystemServer.java

SyetemServer run

   private void run() {
        Looper.prepareMainLooper();

        // Initialize native services.
        System.loadLibrary("android_servers");

        // Create the system service manager.
        mSystemServiceManager = new SystemServiceManager(mSystemContext);

        // Start services.

        // 引導(dǎo)服務(wù)
        startBootstrapServices();
        // 核心服務(wù)
        startCoreServices();
        // 其他服務(wù)
        startOtherServices();
        
        // Loop forever.
        Looper.loop();
    }

SyetemServer 啟動了一堆服務(wù),這些服務(wù)由 mSystemServiceManager進行創(chuàng)建骗炉、啟動和生命周期管理。
ActivityManagerService 則由此創(chuàng)建:

 /**
     * Starts the small tangle of critical services that are needed to get
     * the system off the ground.  These services have complex mutual dependencies
     * which is why we initialize them all in one place here.  Unless your service
     * is also entwined in these dependencies, it should be initialized in one of
     * the other functions.
     */
    private void startBootstrapServices() {
        // Wait for installd to finish starting up so that it has a chance to
        // create critical directories such as /data/user with the appropriate
        // permissions.  We need this to complete before we initialize other services.
        Installer installer = mSystemServiceManager.startService(Installer.class);

        // Activity manager runs the show.
        mActivityManagerService = mSystemServiceManager.startService(
                ActivityManagerService.Lifecycle.class).getService();
        mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
        mActivityManagerService.setInstaller(installer);

        // Set up the Application instance for the system process and get started.
        mActivityManagerService.setSystemProcess();

        // The sensor service needs access to package manager service, app ops
        // service, and permissions service, therefore we start it after them.
        startSensorService();
    }

注意1:mSystemServiceManagerstartService(Class<T> serviceClass),包含了創(chuàng)建,注冊部默,啟動service的過程:

  /**
     * Creates and starts a system service. The class must be a subclass of
     * {@link com.android.server.SystemService}.
     *
     * @param serviceClass A Java class that implements the SystemService interface.
     * @return The service instance, never null.
     * @throws RuntimeException if the service fails to start.
     */
    @SuppressWarnings("unchecked")
    public <T extends SystemService> T startService(Class<T> serviceClass) {
        try {
            final String name = serviceClass.getName();
            Slog.i(TAG, "Starting " + name);
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);

            // Create the service.
            if (!SystemService.class.isAssignableFrom(serviceClass)) {
                throw new RuntimeException("Failed to create " + name
                        + ": service must extend " + SystemService.class.getName());
            }
            final T service;
            try {
                Constructor<T> constructor = serviceClass.getConstructor(Context.class);
                service = constructor.newInstance(mContext);
            } catch (InstantiationException ex) {
                throw new RuntimeException("Failed to create service " + name
                        + ": service could not be instantiated", ex);
            } catch (IllegalAccessException ex) {
                throw new RuntimeException("Failed to create service " + name
                        + ": service must have a public constructor with a Context argument", ex);
            } catch (NoSuchMethodException ex) {
                throw new RuntimeException("Failed to create service " + name
                        + ": service must have a public constructor with a Context argument", ex);
            } catch (InvocationTargetException ex) {
                throw new RuntimeException("Failed to create service " + name
                        + ": service constructor threw an exception", ex);
            }

            // Register it.
            mServices.add(service);

            // Start it.
            try {
                service.onStart();
            } catch (RuntimeException ex) {
                throw new RuntimeException("Failed to start service " + name
                        + ": onStart threw an exception", ex);
            }
            return service;
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
        }
    }

注意2:mActivityManagerService.setSystemProcess():

public void setSystemProcess() {
        try {
            ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
            ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
            ServiceManager.addService("meminfo", new MemBinder(this));
            ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
            ServiceManager.addService("dbinfo", new DbBinder(this));
            if (MONITOR_CPU_USAGE) {
                ServiceManager.addService("cpuinfo", new CpuBinder(this));
            }
            ServiceManager.addService("permission", new PermissionController(this));
            ServiceManager.addService("processinfo", new ProcessInfoService(this));

            ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(
                    "android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
            mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());

         
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(
                    "Unable to find android system package", e);
        }
    }

Activity 啟動流程

Activity 啟動流程

PlantUML 文件記錄

  1. Activity 成長路程
@startuml Activity 成長路程
Activity->Activity:startActivity
Activity->Activity:startActivityForResult
Activity->Activity:startActivityForResult

Activity->Instrumentation:execStartActivity

Instrumentation->ActivityManagerService:startActivity
ActivityManagerService->ActivityManagerService:startActivityAsUser
ActivityManagerService->ActivityStarter:startActivityMayWait
ActivityStarter->ActivityStarter:startActivityLocked
ActivityStarter->ActivityStarter:startActivityUnchecked

ActivityStarter->ActivityStack:startActivityLocked
ActivityStarter->ActivityStackSupervisor:resumeFocusedStackTopActivityLocked
ActivityStackSupervisor->ActivityStack:resumeTopActivityUncheckedLocked
ActivityStack->ActivityStack:resumeTopActivityInnerLocked
ActivityStack->ActivityStackSupervisor:startSpecificActivityLocked
ActivityStackSupervisor->ActivityStackSupervisor:realStartActivityLocked
ActivityStackSupervisor->ApplicationThread:scheduleLaunchActivity


ApplicationThread->ActivityThread:sendMessage(H.LAUNCH_ACTIVITY)
ActivityThread->ActivityThread:handleLaunchActivity
ActivityThread->ActivityThread:performLaunchActivity
ActivityThread->Instrumentation:callActivityOnCreate

Instrumentation->Instrumentation:prePerformCreate
Instrumentation->Activity:performCreate
Activity->Activity:onCreate

ActivityThread->ActivityThread:handleResumeActivity
ActivityThread->ActivityThread:performResumeActivity
ActivityThread->Activity:performResume
Activity->Activity:performRestart
Activity->Instrumentation:callActivityOnRestart
Instrumentation->Activity:onRestart
Activity->Instrumentation:callActivityOnResume
Instrumentation->Activity:onResume

ActivityThread->Activity:makeVisibale



@enduml

參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末甫匹,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子撇贺,更是在濱河造成了極大的恐慌,老刑警劉巖造成,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件显熏,死亡現(xiàn)場離奇詭異,居然都是意外死亡晒屎,警方通過查閱死者的電腦和手機喘蟆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鼓鲁,“玉大人蕴轨,你說我怎么就攤上這事『Э裕” “怎么了橙弱?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我棘脐,道長斜筐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任蛀缝,我火速辦了婚禮顷链,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘屈梁。我一直安慰自己嗤练,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布在讶。 她就那樣靜靜地躺著煞抬,像睡著了一般。 火紅的嫁衣襯著肌膚如雪构哺。 梳的紋絲不亂的頭發(fā)上革答,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天,我揣著相機與錄音遮婶,去河邊找鬼蝗碎。 笑死,一個胖子當(dāng)著我的面吹牛旗扑,可吹牛的內(nèi)容都是我干的蹦骑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼臀防,長吁一口氣:“原來是場噩夢啊……” “哼眠菇!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起袱衷,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤捎废,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后致燥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體登疗,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年嫌蚤,在試婚紗的時候發(fā)現(xiàn)自己被綠了辐益。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡脱吱,死狀恐怖智政,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情箱蝠,我是刑警寧澤续捂,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布垦垂,位于F島的核電站,受9級特大地震影響牙瓢,放射性物質(zhì)發(fā)生泄漏劫拗。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一一罩、第九天 我趴在偏房一處隱蔽的房頂上張望杨幼。 院中可真熱鬧,春花似錦聂渊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至找蜜,卻和暖如春饼暑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背洗做。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工弓叛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人诚纸。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓撰筷,卻偏偏與公主長得像,于是被迫代替她去往敵國和親畦徘。 傳聞我的和親對象是個殘疾皇子毕籽,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容