Android技术知识Android知识手机移动程序开发

Android 系统启动流程总结

2017-01-12  本文已影响1783人  VitaminChen

1. 从 init 开始(init.cpp)

init 进程是用户空间启动的第一个进程,init 通过:
解析 init.rc 文件 -> 创建 Service 对象 -> Service.start() -> fork() -> execve() 来启动 zygote 进程

2. zygote 启动 (app_main.cpp)

在 zygote 的 main() 函数中主要做了下面几件事

这里重点是最后一步,start() 函数里几个重要的步骤是:

3. ZygoteInit 初始化

4. SystemServer 启动

SytemServer.main() -> SystemServer.run() :

mInstrumentation = new Instrumentation();
ContextImpl context = ContextImpl.createAppContext(this, getSystemContext().mPackageInfo);
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
mInitialApplication.onCreate();```
  * mSystemContext = activityThread.getSystemContext() -> ContextImpl.createSystemContext(),关键代码如下:

static ContextImpl createSystemContext(ActivityThread mainThread) {
LoadedApk packageInfo = new LoadedApk(mainThread);
ContextImpl context = new ContextImpl(null, mainThread,
packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
context.mResourcesManager.getDisplayMetrics());
return context;
}

* startBootstrapServices() 、startCoreServices() 、startOtherServices() 启动各类系统服务,这些服务都通过 SystemServiceManager.startService() 或 ServiceManager.addService() 来启动和管理。启动进行的不同的阶段,会多次调用 SystemServiceManager.startBootPhase(int phase) -> service.onBootPhase(phase),传入不同的 phase 值,不同的服务根据 phase 值不同可以做对应的处理。
在 startOtherServices() 的最后,会调用 ActivityManagerService.systemReady(),这里面会调用 startHomeActivityLocked() 来启动系统的第一个 app,也就是 Lanuncher。
* Looper.loop()  至此,系统的启动部分逻辑就全部结束了
上一篇 下一篇

猜你喜欢

热点阅读