最近回顾的一些知识,补充了一下。

源码标准: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的开机流程结束。

posted on 2022-02-10 13:32  翻滚的咸鱼  阅读(702)  评论(0编辑  收藏  举报