引言: 由于上節(jié)我們知道SystemServer啟動了很多系統(tǒng)服務,當前也啟動了ActivityManagerService服務 所以我們這節(jié)內(nèi)容就分析AMS的啟動過程以及在系統(tǒng)中承擔的角色.
@(晚輩目前還是在校大學生,所以可能又很多地方理解有偏差,所以希望前輩們不吝賜教)
ActivityManagerService的創(chuàng)建1startService2ActivityManagerServiceLifecycleclassAMSAMSonStartsetSystemPRocess
銜接上文: 在SystemServer的run方法中調(diào)用startBootstrapServices(),在其中:
// Activity manager runs the show. mActivityManagerService = mSystemServiceManager.startService( ActivityManagerService.Lifecycle.class).getService();//[1][2] //給AMS設置SystemServiceManager mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); ... mActivityManagerService.setSystemProcess();@(SystemServiceManager.java->startService())
通過反射調(diào)用構(gòu)造 調(diào)用service.onStart()方法 public <T extends SystemService> T startService(Class<T> serviceClass) { try { final String name = serviceClass.getName(); ... final T service; try { Constructor<T> constructor = serviceClass.getConstructor(Context.class); service = constructor.newInstance(mContext); } ... mServices.add(service); // Start it. try { service.onStart();//針對AMS,則調(diào)用AMS的onStart()方法 } ... return service; } finally { ... } }@(SystemService的派生類)
內(nèi)部持有AMS的對象,所以到目前為止AMS就創(chuàng)建出來了
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; } }@(AMS的構(gòu)造方法)
創(chuàng)建出一些管理4大組件的管理對象.
// public ActivityManagerService(Context systemContext) { mContext = systemContext; mFactoryTest = FactoryTest.getMode(); //獲取ActivityThread對象的引用 mSystemThread = ActivityThread.currentActivityThread(); //創(chuàng)建一個HandlerThread mHandlerThread = new ServiceThread(TAG, android.os.Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/); mHandlerThread.start(); mHandler = new MainHandler(mHandlerThread.getLooper()); //創(chuàng)建UI線程 mUiHandler = new UiHandler(); ... //創(chuàng)建用于管理廣播的數(shù)據(jù)結(jié)構(gòu) mFgBroadcastQueue = new BroadcastQueue(this, mHandler, "foreground", BROADCAST_FG_TIMEOUT, false); mBgBroadcastQueue = new BroadcastQueue(this, mHandler, "background", BROADCAST_BG_TIMEOUT, true); mBroadcastQueues[0] = mFgBroadcastQueue; mBroadcastQueues[1] = mBgBroadcastQueue; mServices = new ActiveServices(this);//創(chuàng)建管理Service的組件 mProviderMap = new ProviderMap(this);//創(chuàng)建管理provider的組件 mAppErrors = new AppErrors(mContext, this); // 創(chuàng)建一個/data/system的目錄 File dataDir = Environment.getDataDirectory(); File systemDir = new File(dataDir, "system"); systemDir.mkdirs(); //有關(guān)電池相關(guān)的服務操作 mBatteryStatsService = new BatteryStatsService(systemDir, mHandler); mBatteryStatsService.getActiveStatistics().readLocked(); mBatteryStatsService.scheduleWriteToDisk(); mOnBattery = DEBUG_POWER ? true : mBatteryStatsService.getActiveStatistics().getIsOnBattery(); mBatteryStatsService.getActiveStatistics().setCallback(this); //有關(guān)進程統(tǒng)計的服務 mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats")); mAppOpsService = new AppOpsService(new File(systemDir, "appops.xml"), mHandler); mAppOpsService.startWatchingMode(AppOpsManager.OP_RUN_IN_BACKGROUND, null, new IAppOpsCallback.Stub() { @Override public void opChanged(int op, int uid, String packageName) { if (op == AppOpsManager.OP_RUN_IN_BACKGROUND && packageName != null) { if (mAppOpsService.checkOperation(op, uid, packageName) != AppOpsManager.MODE_ALLOWED) { runInBackgroundDisabled(uid); } } } }); ... //有關(guān)cpu信息的初始化 mProcessCpuTracker.init(); mCompatModePackages = new CompatModePackages(this, systemDir, mHandler); mIntentFirewall = new IntentFirewall(new IntentFirewallInterface(), mHandler); //創(chuàng)建管理Activity的對象 mStackSupervisor = new ActivityStackSupervisor(this); mActivityStarter = new ActivityStarter(this, mStackSupervisor); mRecentTasks = new RecentTasks(this, mStackSupervisor); //創(chuàng)建一個線程名稱為CpuTracker,主要是做一些CPU有關(guān)的操作 mProcessCpuThread = new Thread("CpuTracker") { @Override public void run() { while (true) { try { try { synchronized(this) { final long now = SystemClock.uptimeMillis(); long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now; long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now; if (nextWriteDelay < nextCpuDelay) { nextCpuDelay = nextWriteDelay; } if (nextCpuDelay > 0) { mProcessCpuMutexFree.set(true); this.wait(nextCpuDelay); } } } catch (InterruptedException e) { } updateCpuStatsNow(); } catch (Exception e) { Slog.e(TAG, "Unexpected exception collecting process stats", e); } } } }; ... }@(onStart())
private void start() { Process.removeAllProcessGroups();//Remove all process groups mProcessCpuThread.start();//Runtime CPU use collection thread //啟動cpu相關(guān) mBatteryStatsService.publish(mContext); mAppOpsService.publish(mContext); LocalServices.addService(ActivityManagerInternal.class, new LocalService());}@(源于SystemService中startBootstrapServices()調(diào)用)
mActivityManagerService.setSystemProcess(); 主要是向ServiceManager注冊了一些服務,最后看出向AMS中把SystemServer自己添加到其中,所以AMS是管理這個系統(tǒng)進程的類.
public void setSystemProcess() { try { //將AMS添加到ServiceManager ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true); //添加進程信息服務 ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats); //添加每個進程內(nèi)存使用服務 ServiceManager.addService("meminfo", new MemBinder(this)); //添加每個進程圖形加速卡狀態(tài)服務 ServiceManager.addService("gfxinfo", new GraphicsBinder(this)); //db狀態(tài)服務 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)); ... synchronized (this) { //把SystemServer進程本身添加到process管理中 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( "Unable to find android system package", e); } }新聞熱點
疑難解答