1.解析SystemServer进程
我们来看下SystemServer的mai函数
public static void mian(String[] args){
new SystemServer().run();
}
main函数只调用了run函数
private void run() {
...
System.loadLibrary("android_servers");//1.加载so库
...
//2.创建 SystemServiceManager, 创建并启动系统的各种服务
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
...
try {
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices");
startBootstrapServices();//3.启动了AMS ,ATMS,PMS ...
startCoreServices();//4.启动BatteryService,WebViewUpdateService,GpuService...
//5.启动WMS,CameraService,BluetoothService,AudioService,LocationManagerService
startOtherServices();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
...
}
看下startBootstrapServices函数
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
//1.创建并启动ATMS ,ServiceManager.addService("activity_task",atm)
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
// 2.创建并启动AMS
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
// 注册binder,ServiceManager.addService("activity",mActivityManagerService)
mActivityManagerService.setSystemProcess();
//3.创建并注册PMS, 注册binder, ServiceManager.addService("package", iPackageManager);
PackageManagerService.main()
}
看下startOtherServices函数
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
//创建WMS,并启动
wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
//注册binder服务
ServiceManager.addService(Context.WINDOW_SERVICE, wm, false,
DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
// AMS和WMS关联上
mActivityManagerService.setWindowManager(wm);
}
2.ATMS和AMS的区别
AMS以前是管理四大组件,太庞大了,所以为了给AMS减压,把对Activity的管理抽到了ATMS.