启动Binder线程池和SystemServiceManager,并且启动各种系统服务
SystemServer.main()
初始化SystemServer对象,然后调用run()
new SystemServer().run()
SystemServer.run()
//其他代码省略
createSystemContext();//加载系统资源
startBootstrapServices(t);//启动引导服务
startCoreServices(t);//启动核心服务
startOtherServices(t);//启动其他服务
SystemServer.createSystemContext()
//系统资源加载
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();//ContextImpl
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
ActivityThread.systemMain()
//ResourcesManager.getInstance()获取资源管理实例
ActivityThread thread = new ActivityThread();
thread.attach(true, 0);
return thread;
thread.attach(true, 0);
mInstrumentation = new Instrumentation();
mInstrumentation.basicInit(this);
/**
getSystemContext()单例模式创建ContextImpl对象mSystemContext-->createSystemContext-->创建LoadedApk对象(创建ApplicationInfo(),创建ClassLoader)
createAppContext()利用刚创建的LoadedApk对象创建新的ContextImpl对象
**/
ContextImpl context = ContextImpl.createAppContext(this,getSystemContext().mPackageInfo);
/**
initializeJavaContextClassLoader() 设置当前的线程ContextClassLoader
newApplication()
public Application newApplication(ClassLoader cl, String className, Context context)throws InstantiationException, IllegalAccessException, ClassNotFoundException {
//创建Application对象
Application app = getFactory(context.getPackageName())
.instantiateApplication(cl, className);
//将新创建的ContextImpl对象保存到Application父类成员变量mBase
//将新创建的LoadedApk对象保存到Application的成员变量mLoadedApk
app.attach(context);
return app;
}
**/
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
mInitialApplication.onCreate();
SystemServer.startBootstrapServices()
// SystemServiceManager 专门管理各种服务启动(java层各种服务)
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
// 在SystemServiceManager.startService()中new Lifecycle()-->new ActivityManagerService(),且回调Lifecycle.onStart()
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
//设置AMS的APP安装器
mActivityManagerService.setInstaller(installer);
//开启PMS服务
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
//初始化AMS相关的PMS服务
mActivityManagerService.initPowerManagement();
//添加C/C++各种服务
mActivityManagerService.setSystemProcess();
new ActivityManagerService(context, sAtm)
- 启动相关服务
- 创建UI线程
- 创建ActiveServices
- 创建CpuTracker线程
Lifecycle.start()
//移除所有的进程组
removeAllProcessGroups();
//启动CpuTracker线程
mProcessCpuThread.start();
//启动电池统计服务
mBatteryStatsService.publish();
//启动APP操作信息服务
mAppOpsService.publish();
//添加到LocalServices中
LocalServices.addService(ActivityManagerInternal.class, mInternal);
ActivityManagerService.setSystemProcess();
/**
ServiceManager c/c++服务
activity AMS
procstats 进程统计
meminfo 内存信息
gfxinfo 图像信息
dbinfo 数据库
cpuinfo
permission
processinfo 进程信息
cacheinfo 缓存信息
**/
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, /* allowIsolated= */ true,DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PROTO);
ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
ServiceManager.addService("meminfo", new MemBinder(this), /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_HIGH);
ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
ServiceManager.addService("dbinfo", new DbBinder(this));
if (MONITOR_CPU_USAGE) {
ServiceManager.addService("cpuinfo", new CpuBinder(this),
/* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
}
ServiceManager.addService("permission", new PermissionController(this));
ServiceManager.addService("processinfo", new ProcessInfoService(this));
ServiceManager.addService("cacheinfo", new CacheBinder(this));
/**
getSystemContext().installSystemApplicationInfo(info, classLoader);
getSystemUiContext().installSystemApplicationInfo(info, classLoader);
mProfiler = new Profiler();
**/
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
//创建ProcessRecord对象
ProcessRecord app = mProcessList.newProcessRecordLocked(info, info.processName,
false,
0,
new HostingRecord("system"));
ActivityThread.installSystemApplicationInfo(info,getClass().getClassLoader());
//最终调用LoadedApk的installSystemApplicationInfo(),加载名为android的包
getSystemContext().installSystemApplicationInfo(info, classLoader);
getSystemUiContext().installSystemApplicationInfo(info, classLoader);
//创建用于性能统计Profiler对象
mProfiler = new Profiler();
SystemServer.startOtherServices(t);
//与AMS相关,其他代码省略
/**
安装系统Provider
创建CoreSettingsObserver,用于监控Settings的改变
**/
mActivityManagerService.installSystemProviders();
//
wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,new PhoneWindowManager(),mActivityManagerService.mActivityTaskManager);
//加入到底层服务中
ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
//WMS管理
mActivityManagerService.setWindowManager(wm);
/**
startSystemUi()启动系统UI
执行一系列服务的systemReady()
**/
mActivityManagerService.systemReady();
//至此80多个服务初始化完成