一.ActivityManagerService(AMS) 啟動過程分析
在SystemServer啟動ActivityManagerService
如果想了解SystemServer啟動過程可以看這篇文章:Android 源碼分析(六) SystemServer 進程
frameworks\base\services\java\com\android\server\SystemServer.java // Activity manager runs the show. traceBeginAndSlog("StartActivityManager"); mActivityManagerService = mSystemServiceManager.startService( ActivityManagerService.Lifecycle.class).getService(); mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer);
frameworks\base\services\core\java\com\android\server\am\ActivityManagerService.java
public class ActivityManagerService extends IActivityManager.Stub implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { /** All system services */ SystemServiceManager mSystemServiceManager; /** Run all ActivityStacks through this */ //管理Activity final ActivityStackSupervisor mStackSupervisor; final ActivityStarter mActivityStarter; final TaskChangeNotificationController mTaskChangeNotificationController; final InstrumentationReporter mInstrumentationReporter = new InstrumentationReporter(); final ArrayList<ActiveInstrumentation> mActiveInstrumentation = new ArrayList<>(); // Whether we should use SCHED_FIFO for UI and RenderThreads. private boolean mUseFifoUiScheduling = false; //Broadcast 廣播 ,前台廣播隊列和后台廣播隊列 BroadcastQueue mFgBroadcastQueue; BroadcastQueue mBgBroadcastQueue; // Convenient for easy iteration over the queues. Foreground is first // so that dispatch of foreground broadcasts gets precedence. final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2]; BroadcastStats mLastBroadcastStats; BroadcastStats mCurBroadcastStats; BroadcastQueue broadcastQueueForIntent(Intent intent) { final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0; if (DEBUG_BROADCAST_BACKGROUND) Slog.i(TAG_BROADCAST, "Broadcast intent " + intent + " on " + (isFg ? "foreground" : "background") + " queue"); return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue; } //Activity 堆棧 /** * The last resumed activity. This is identical to the current resumed activity most * of the time but could be different when we're pausing one activity before we resume * another activity. */ private ActivityRecord mLastResumedActivity; /** * If non-null, we are tracking the time the user spends in the currently focused app. */ private AppTimeTracker mCurAppTimeTracker; //ANR ? 最后個ANR狀態,難道可以記錄app發生ANR的? /** * Dump of the activity state at the time of the last ANR. Cleared after * {@link WindowManagerService#LAST_ANR_LIFETIME_DURATION_MSECS} */ String mLastANRState; //Service 和 Provider 管理 final ActiveServices mServices; final ProviderMap mProviderMap; //存放系統數據目錄 // TODO: Move creation of battery stats service outside of activity manager service. File dataDir = Environment.getDataDirectory(); File systemDir = new File(dataDir, "system"); systemDir.mkdirs(); mBatteryStatsService = new BatteryStatsService(systemDir, mHandler); //應用權限管理 mAppOpsService = mInjector.getAppOpsService(new File(systemDir, "appops.xml"), mHandler); //AcitivityManager 添加進來 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)); //最后 使用Watchdog監控 Watchdog.getInstance().addMonitor(this); Watchdog.getInstance().addThread(mHandler); }
從上面截取的一些代碼片段,我們能了解到, AMS創建過程 涉及到Android 四大組件管理的初始化工作。並且ActivityManagerService extends IActivityManager.Stub,所以可知AcitivityManagerService與AcitivityManager之間通信也是使用binder機制。
進ActivityManager 里面看看
//與ActivityManagerService里的ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);對應。 @SystemService(Context.ACTIVITY_SERVICE) public class ActivityManager { ... }
二.ActivityManager和ActivityManagerService關系
如果想了解Activity是如果通過ActivityManager調用ActivityManagerService的過程可以看下這篇文章.
ActivityManager(frameworks/base/core/java/android/app/ActivityManager.java)
ActivityManager 是客戶端用來管理系統中正在運行的所有Activity.
上層APP通過ActivityManager使用binder機制傳遞信息給AMS,由AMS去完成交互和調度工作后通過binder機制返回給ActivityManager,把結果在返回給上層app。
一張圖了解ActivityManager和ActivityManagerService在整個Android系統通信過程中位置。
參考:
[1]https://www.cnblogs.com/bastard/p/5770573.html