Android系统启动流程
简单描述:
- 1 按下电源键开机,启动芯片代码,启动一个BootLoader小程序。
- 2 BootLoader主要的功能是完成系统的设置,启动init.rc,init.cpp,init.zygote32.rc(8.0以后)
- 3 init 启动zygote:通过rc文件,service.cpp找到相关的name,对应的main方法。即app_main.cpp的main方法。
- 4 zygote启动SystemServer
Zygote开启了Android世界。
app_main.cpp是启动zygote进程的方法。
AndroidRuntime.start() ----(启动虚拟机,)
启动ZygoteInit的main方法执行。主要做了以下四件事
- 创建一个Server端的socket(LocalServerSocket)。
- 预加载类和资源。
- 启动SystemServer进程。
- 等待AMS请求创建新的应用程序进程。
启动SystemServer, —> RuntimeInit.zygoteInit
—>启动Binder线程池,这样就可以和其他进程通讯了,nativeZygoteInit();
—>执行SystemServer的main方法,applicationInit(targetSdkVersion, argv, classLoader)
- 5 SystemServer启动主线程,以及其他的系统服务。
开启looper,创建mSystemServiceManager,启动服务器的小助手,
启动引导服务,核心服务,其他服务
引导服务 | installer | |
引导服务 | ActivityManagerService | 赋值install,systemServiceManager |
引导服务 | PowerManagerService | initPowerManager |
引导服务 | LightsService | 启动服务 |
引导服务 | DisplayManagerService | 在packagemanagerservice之前提供 |
引导服务 | PackageManagerService | 是不是第一次boot,获取packagemanager |
引导服务 | UserManagerService | |
引导服务 | AttributeCache.init(mSystemContext) | |
引导服务 | mActivityManagerService.setSystemProcess() | |
引导服务 | startSensorService(); |
服务太多了,不一一描述了。
服务的启动有两种,一种是SystemServiceManager创建并启动的服务,都是继承了SystemService,但是并没有实现IBinder,所以是不可以进行Binder通信的,也就是由它管理的服务是用于进程内部通信的。
另一种是ServiceManager.addService(“package”, m);之类的。
ServiceManager是用来管理系统中各种Service的,由它管理的服务都实现了IBinder,所以在ServiceManager中注册的服务是用于进程间通信的:用于系统C/S架构中的Binder通信机制。客户端要使用某个Service,需要先到ServiceManager中查询Service的信息,然后根据该信息与Service所在的Server进程建立通信,这样客户端就可以使用Service了。
SystemServer进程主要做一下几件事
- 启动Binder线程池,这样就可以与其他进程进行通信了。
- 创建SystemServiceManager,用于对系统其他服务进行创建、启动和声明周期管理。
- 启动各种系统服务。
Launcher启动过程
SystemServer启动主线程,以及其他的系统服务。有一个方法startOtherService
mActivityManagerService.systemReady(new Runnable() {
public void run(){
}
}
由ActivityManagerService的systemReady。
public void systemReady(final Runnable goingCallback) {
//省略
startHomeActivityLocked(currentUserId, "systemReady");
//省略
}
boolean startHomeActivityLocked(int userId, String reason) {
//判断系统的运行模式和mTopAction的值
if (mFactoryTest == FactoryTest.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;
}
//获取Luncher启动所需要的Intent
Intent intent = getHomeIntent();
ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
if (aInfo != null) {
intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
// Don't do this if the home app is currently being
// instrumented.
aInfo = new ActivityInfo(aInfo);
aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
ProcessRecord app = getProcessRecordLocked(aInfo.processName,
aInfo.applicationInfo.uid, true);
if (app == null || app.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
//启动Luncher
mActivityStarter.startHomeActivityLocked(intent, aInfo, reason);
}
} else {
Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
}
return true;
}