Android面试季必问——AMS的核心原理 系列
Android启动流程 https://www.jianshu.com/p/f66f639c59d3
AMS启动和应用 https://www.jianshu.com/p/8f0d372b0a34
Activity启动流程 https://www.jianshu.com/p/53d63374619d
AMS启动
四大组件启动、平时的命令服务都来源于AMS。
还是从开机开始:

Zygote进程启动:

App由Zygote拉起,Zygote先孵化出system server(第一个)拉起各种服务包括launch桌面服务,最后由AMS启动桌面服务。
AMS由SystemServer拉起!
*面试问题:App为什么从zygote 中创建进程,为什么不从init->zygote->system_server中的 init或System_service中创建?
1,init时耗时,加载虚拟机,预加载类文件。等耗时操作。
2,system_server要创建大量服务(80左右),app不需要。
在SS中 的run方法里有如下引导服务步骤启动AMS:


AMS启动经历了哪一些过程:
SS中:

他调用了一个mSystemServiceManager;
*这个类和Service_manager容易混淆。
Service_manager管理的是Binder相关的服务。
SystemServiceManager;是系统相关的服务。

SystemServicer把启动分为了不同的阶段(因为启动时间太长了,下面的服务需要上面的服务启动完毕)

回到SS中 创建mActivityManagerService传了一个
mActivityManagerService.Lifecycle.class对象,通过反射把service创建。
mActivityManagerService.Lifecycle是AMS的静态内部类

AMS启动中都干了些什么
部分代码:

重要的有:mUiHandler(ui主线程)、mProcessStats(应用进程信息)、mStackSupervisor(任务栈管理,管理activity栈)、mLifecycleManager(Activity启动时用到,下一节)、Watchdog(看门狗。守护进程)、updateOomAdjLocked()(此方法跟进程保活相关)
AMS创建之后哪里用到了
回到开始图片

设置安装程序的安装器installer


在这里,把上面服务加到ServiceManager中管理,供我们使用。
网友评论