- 所有进程都是由init进程直接或间接fork出来的
- android系统启动时,init进程会fork出zygote,意为“受精卵”,后面的所有进程都是zygote分裂出来的
- 在zygote进程初始化时会启动SystemServer进程,平时所用到的AMS、PMS、WMS、网络等服务都是在SystemServer启动时开启的,SystemServer中启动服务的代码
private void run() {
// Initialize the system context.
createSystemContext();
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// Start services.
try {
startBootstrapServices();
startCoreServices();
startOtherServices();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
}
}
进行了系统ActivityThread的创建,即开启了launcher进程
- 手机开机完成这一过程,就已经有4个进程在跑着,分别是init、zygote、SystemServer、launcher进程
- 点击桌面的应用图标,就在launcher所在activity中启动app的入口activity,并且是新开了一个栈。具体流程是,通过AMS的代理对象告诉AMS要启动一个activity,AMS通知zygote进程fork一个新进程,并且AMS通过ApplicationThreadProxy通知app进程要启动一个activity,最终的逻辑实现也是通过AMS代理通知AMS进行操作就会调用app的ActivityThread的performLaunchActivity启动页面,AMS也会通过ApplicationThreadProxy通知launcher进程pause掉原来页面
- 整个启动流程,AMS更像是一个桥梁的角色,负责管理着各个进程的页面
- app启动的入口是ActivityThread的main方法,里面调用了AMS的attachApplication方法,让AMS可以拿到进程的ApplicationThread引用,而ApplicationThread是一个Binder对象,这样,AMS就能通过ApplicationThread管理activity
final IActivityManager mgr = ActivityManagerNative.getDefault();
try {
mgr.attachApplication(mAppThread);
} catch (RemoteException ex) {
// Ignore
}
2126

被折叠的 条评论
为什么被折叠?



