最近回顧的一些知識,補充了一下。
源碼標准:API : 29「Android 10.0」
android手機是怎么開機的?
android 的底層是 linux kernel「內核」,由 BootLoader「系統啟動加載器」 負責加載(類似於計算機的BIOS系統)。
/bootable/recovery/bootloader.h
首先啟動 init「父進程,第一個進程」進程,接着運行init.rc腳本,腳本文件有個命令啟動了Zygote進程,初始化時會啟動虛擬機。
/system/core/rootdir/init.zygote.rc
Zygote進程fork出SystemServer進程,然后會調用SystemServer.main()方法。
/frameworks/base/services/java/com/android/server/SystemService.java
/** The main entry point from zygote.*/ public static void main(String[] args) { new SystemServer().run(); }
run方法中,主要是在進程中啟動系統的各項服務,比如ActivityManagerService,PackageManagerService,WindowManagerService服務等。
private void run() { //創建主線程Looper、ActivityThread、SystemContext android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND); Looper.prepareMainLooper(); // Initialize native services. System.loadLibrary("android_servers"); // Initialize the system context. createSystemContext(); // Create the system service manager. mSystemServiceManager = new SystemServiceManager(mSystemContext); mSystemServiceManager.setStartInfo(mRuntimeRestart,mRuntimeStartElapsedTime, mRuntimeStartUptime); LocalServices.addService(SystemServiceManager.class, mSystemServiceManager); // 並行線程池 SystemServerInitThreadPool.get(); // Start services. traceBeginAndSlog("StartServices"); startBootstrapServices(); startCoreServices(); startOtherServices(); // Loop forever. Looper.loop(); }
下面是一些主要的初始化方法。
/** * 這些服務具有復雜的相互依賴關系,所以需要放一起全部初始化 */ private void startBootstrapServices() { // Start the watchdog as early as possible so we can crash the system server final Watchdog watchdog = Watchdog.getInstance(); watchdog.start(); //啟動AMS ActivityTaskManagerService atm = mSystemServiceManager.startService( ActivityTaskManagerService.Lifecycle.class).getService(); mActivityManagerService = ActivityManagerService.Lifecycle.startService( mSystemServiceManager, atm); //電源管理器需要提前啟動,因為其他服務需要它 mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class); // Start the package manager. mPackageManagerService = PackageManagerService.main(mSystemContext, installer, mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore); //設置Application實例並開始 mActivityManagerService.setSystemProcess(); //使用 ActivityManager 實例完成看門狗設置並監聽重啟 watchdog.init(context, mActivityManagerService); }
真正啟動是在ActivityManagerService的中systemReady方法,調用resumeTopActivityLocked打開鎖屏界面。
/** * Starts a miscellaneous grab bag of stuff that has yet to be refactored and organized. */ private void startOtherServices() { //啟動WMS wm = WindowManagerService.main(); mActivityManagerService.setWindowManager(wm); //WMS 顯示默認啟動消息 ActivityManagerNative.getDefault().showBootMessage(); //開始啟動初始應用程序 mActivityManagerService.systemReady(new Runnable(){ //SystemUI startSystemUi(context, windowManagerF); }); }
/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
/** 通過StackSupervisor運行所有 ActivityStacks */ final ActivityStackSupervisor mStackSupervisor; public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) { mStackSupervisor.resumeFocusedStackTopActivityLocked(); }
到這里,android的開機流程結束。