Android系統啟動過程

來源:互聯網
上載者:User

Android系統啟動過程

首先看一張Android系統啟動流程圖:

 

一個進程最重要的兩項指標一個是啟動了Binder線程池,也就是可以進程Binder處理序間通訊了。另一個是啟動了Handler訊息迴圈,可以使用了訊息迴圈機制。

 

1、那麼systemserver進程是什麼時候實現上面兩個機制的呢?見代碼:

啟動了Binder線程池。是子線程池。

 

public static final void zygoteInit(String[] argv)            throws ZygoteInit.MethodAndArgsCaller {        ......        zygoteInitNative();        ......}
啟動了Hander訊息迴圈機制,是子線程的Handler:

 

 

public static final void init2() {        Slog.i(TAG, Entered the Android system server!);        Thread thr = new ServerThread();        thr.setName(android.server.ServerThread);        thr.start();    }
class ServerThread extends Thread {@Override    public void run() {        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,            SystemClock.uptimeMillis());        Looper.prepare();        .....                try {                ......            Slog.i(TAG, Activity Manager);            context = ActivityManagerService.main(factoryTest);//初始化了ActivityTask            ......            Slog.i(TAG, Package Manager);            pm = PackageManagerService.main(context,                    factoryTest != SystemServer.FACTORY_TEST_OFF);//初始化了PackageMangerService            ActivityManagerService.setSystemProcess();            .....            wm = WindowManagerService.main(context, power,                    factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);            ServiceManager.addService(Context.WINDOW_SERVICE, wm);            ......        ((ActivityManagerService)ActivityManagerNative.getDefault())                .systemReady(new Runnable() {            public void run() {            }        });                Looper.loop();        Slog.d(TAG, System ServerThread is exiting!);    }}

 

2、那麼應用程式進程是什麼時候實現上面兩個機制的呢?見代碼:

啟動了Binder線程池,和上面一樣,都是子線程池。

 

public static final void zygoteInit(String[] argv)            throws ZygoteInit.MethodAndArgsCaller {        ......        zygoteInitNative();        ......}
啟動了Hander訊息迴圈機制,是主線程的Handler:

 

 

public static final void main(String[] args) {        SamplingProfilerIntegration.start();        Process.setArgV0(
);        Looper.prepareMainLooper();        if (sMainThreadHandler == null) {            sMainThreadHandler = new Handler();        }        ActivityThread thread = new ActivityThread();        thread.attach(false);        ....        Looper.loop();        ......    }

3、我們看一下啟動Laucher介面的程式。
((ActivityManagerService)ActivityManagerNative.getDefault())                .systemReady(new Runnable() {            public void run() {            }        });
參考Android系統原始碼情景分析一書,最後systemReady最終執行到了這塊:

 

 

boolean startHomeActivityLocked() {        if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL                && mTopAction == null) {            // We are running in factory test mode, but unable to find            // the factory test app, so just sit around displaying the            // error message and don't try to start anything.            return false;        }        Intent intent = new Intent(            mTopAction,            mTopData != null ? Uri.parse(mTopData) : null);        intent.setComponent(mTopComponent);        if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {            intent.addCategory(Intent.CATEGORY_HOME);        }        ActivityInfo aInfo =            intent.resolveActivityInfo(mContext.getPackageManager(),                    STOCK_PM_FLAGS);        if (aInfo != null) {            intent.setComponent(new ComponentName(                    aInfo.applicationInfo.packageName, aInfo.name));            // Don't do this if the home app is currently being            // instrumented.            ProcessRecord app = getProcessRecordLocked(aInfo.processName,                    aInfo.applicationInfo.uid);            if (app == null || app.instrumentationClass == null) {                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);                mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo,                        null, null, 0, 0, 0, false, false);            }        }                        return true;    }
這是在SystemServer的子線程中執行的,mMainStack.startActivityLocked的執行代碼,請參考Android系統原始碼情景分析一書,執行不同出現在resumeTopActivityLocked這個方法中,由於mResumedActivity等於NULL,所以此時執行,startSpecificActivityLocked(next, true, true)。最後執行到startProcessLocked,裡面這句話是關鍵。

 

 

int pid = Process.start(android.app.ActivityThread,                    mSimpleProcessManagement ? app.processName : null, uid, uid,                    gids, debugFlags, null);
Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);                    msg.obj = app;                    mHandler.sendMessageDelayed(msg, PROC_START_TIMEOUT);
其中mHander為final Handler mHandler = new Handler() {},這個Handler是ServerThread子線程中的handler。發送訊息時,只調用了Looper.prepare();返回後才調用Looper.loop();

 

Process.start建立了一個應用程式進程,開啟了Binder線程池和Handler訊息迴圈機制,請參考第2點。

1、thread.attach(false)是Launcher主線程與SystemServer子線程Binder處理序間通訊,發送ATTACH_APPLICATION_TRANCTION;SystemServer子線程(Binder線程池)移除SystemServer中的ServerThread子線程(Handler訊息迴圈)裡面的Handler裡面的訊息。

2、SystemServer子線程向Laumcher子線程發送SCHEDULE_LAUNCH_ACTIVITY_TARNSACTION;之後SystemServer子線程繼續迴圈等待,並向Launcher子線程發送返回資料。

3、Laucher主線程返回後調用了Looper.loop(),主線程訊息迴圈機制正式創立。

4、處理剛才由SystemServer子線程發送Lanncher子線程的資料(單向),Launcher子線程最後通過訊息處理機制,在主線程調用了Launcher的onCreate方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.