startBootstrapServices
在SystemServer.run方法中startBootstrapServices()
1 2 3 4 5 6 7 8 9 10 11 12 13
| private void startBootstrapServices() { Installer installer = mSystemServiceManager.startService(Installer.class); //启动AMS服务 mActivityManagerService = mSystemServiceManager.startService( ActivityManagerService.Lifecycle.class).getService(); //设置AMS的系统服务管理器 mActivityManagerService.setSystemServiceManager(mSystemServiceManager); //设置AMS的APP安装器 mActivityManagerService.setInstaller(installer); ... //设置SystemServer mActivityManagerService.setSystemProcess(); }
|
mSystemServiceManager.startService(ActivityManagerService.Lifecycle.class)
功能:
- 创建ActivityManagerService.Lifecycle对象;
- 调用Lifecycle.onStart()方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public static final class Lifecycle extends SystemService { private final ActivityManagerService mService;
public Lifecycle(Context context) { super(context); mService = new ActivityManagerService(context); }
@Override public void onStart() { mService.start(); }
public ActivityManagerService getService() { return mService; } }
|
在Lifecycle中创建AMS实例对象,并调用AMS.start();在new ActivityManagerService(context)创建了三个线程,分别为”ActivityManager”,”android.ui”,”CpuTracker”。
setSystemProcess
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public void setSystemProcess() { try { ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true); ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats); ServiceManager.addService("meminfo", new MemBinder(this)); ServiceManager.addService("gfxinfo", new GraphicsBinder(this)); ServiceManager.addService("dbinfo", new DbBinder(this)); if (MONITOR_CPU_USAGE) { ServiceManager.addService("cpuinfo", new CpuBinder(this)); } ServiceManager.addService("permission", new PermissionController(this)); ServiceManager.addService("processinfo", new ProcessInfoService(this)); ApplicationInfo info = mContext.getPackageManager().getApplicationInfo( "android", STOCK_PM_FLAGS);
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader()); synchronized (this) { ProcessRecord app = newProcessRecordLocked(info, info.processName, false, 0); app.persistent = true; app.pid = MY_PID; app.maxAdj = ProcessList.SYSTEM_ADJ; app.makeActive(mSystemThread.getApplicationThread(), mProcessStats); synchronized (mPidsSelfLocked) { mPidsSelfLocked.put(app.pid, app); } updateLruProcessLocked(app, false, null); updateOomAdjLocked(); } } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException("", e); } }
|
该方法主要就是注册各种服务到ServiceManager。
startOtherServices()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| private void startOtherServices() {
mActivityManagerService.installSystemProviders(); ... mActivityManagerService.systemReady(new Runnable() { public void run() { mSystemServiceManager.startBootPhase( SystemService.PHASE_ACTIVITY_MANAGER_READY);
mActivityManagerService.startObservingNativeCrashes(); WebViewFactory.prepareWebViewInSystemServer(); startSystemUi(context);
networkScoreF.systemReady(); networkManagementF.systemReady(); networkStatsF.systemReady(); networkPolicyF.systemReady(); connectivityF.systemReady(); audioServiceF.systemReady(); Watchdog.getInstance().start();
mSystemServiceManager.startBootPhase( SystemService.PHASE_THIRD_PARTY_APPS_CAN_START);
wallpaper.systemRunning(); inputMethodManager.systemRunning(statusBarF); location.systemRunning(); countryDetector.systemRunning(); networkTimeUpdater.systemRunning(); commonTimeMgmtService.systemRunning(); textServiceManagerService.systemRunning(); assetAtlasService.systemRunning(); inputManager.systemRunning(); telephonyRegistry.systemRunning(); mediaRouter.systemRunning(); mmsService.systemRunning(); } }); }
|
总结
- 创建AMS实例对象,创建Andoid Runtime,ActivityThread和Context对象;
- setSystemProcess:注册AMS、meminfo、cpuinfo等服务到ServiceManager;
- installSystemProviderss,加载SettingsProvider;
- 启动SystemUIService,再调用一系列服务的systemReady()方法;