Android系统服务AMS启动流程揭秘
2023-09-23 20:38:19
Android系统服务AMS启动流程剖析
当Android系统启动时,首先会启动SystemServer进程,然后SystemServer进程会通过反射调用SystemServer.java的mian()方法,在这个方法中,SystemServer会依次执行以下步骤来初始化AMS:
- 创建AMS实例
SystemServer会通过反射创建AMS的实例,并将AMS的实例保存在mAm变量中。
- 初始化AMS
SystemServer会调用AMS的init()方法来初始化AMS,在init()方法中,AMS会执行以下操作:
- 创建Binder服务端点,并注册到ServiceManager中。
- 创建AMS的Binder代理对象,并将其保存在mAmInterface变量中。
- 启动AMS的Binder线程池。
- 启动AMS的Binder线程池
SystemServer会调用AMS的startThreadPool()方法来启动AMS的Binder线程池,在startThreadPool()方法中,AMS会创建Binder线程池,并将线程池保存在mThreadPool变量中。
- 注册AMS的Binder服务端点到ServiceManager
SystemServer会调用ServiceManager的addService()方法将AMS的Binder服务端点注册到ServiceManager中,这样其他进程就可以通过ServiceManager找到AMS的Binder服务端点。
- 将AMS的Binder代理对象保存在mAmInterface变量中
SystemServer会将AMS的Binder代理对象保存在mAmInterface变量中,这样SystemServer就可以通过mAmInterface变量来调用AMS的方法。
- 启动AMS的Binder线程池
SystemServer会调用AMS的startThreadPool()方法来启动AMS的Binder线程池,在startThreadPool()方法中,AMS会创建Binder线程池,并将线程池保存在mThreadPool变量中。
- 完成AMS的初始化
SystemServer会调用AMS的finishInit()方法来完成AMS的初始化,在finishInit()方法中,AMS会执行以下操作:
- 初始化AMS的各种数据结构。
- 启动AMS的定时器线程。
- 将AMS的初始化状态设置为true。
- AMS启动完成
AMS的初始化完成后,SystemServer会继续执行其他初始化操作,当所有初始化操作完成后,SystemServer会启动Zygote进程,Zygote进程会fork出各种应用程序进程。