目录
AMS(ActivityManagerService)的作用
AMS是系统的一个管理和调度服务,服务由SystemService进程启动。应用进程以及四大组件的管理(启动,切换和调度)都是由AMS来实现;
学习大纲
AMS是Android Framework中一个非常重要的系统服务,由于太过于庞大,我的学习思路和大纲是:
一、AMS的启动过程;
二、AMS在应用的启动流程中的角色;
三、Activity生命周期的管理;
四、Broadcast广播的处理流程;
五、Service的启动和管理流程;
六、ContentProvider的管理;
七、AMS中的进程管理;
一、AMS的启动过程
1、AMS是如何启动的?
2、AMS构造方法都做了什么?
3、AMS.start()方法实现
4、AMS设置系统进程实现
5、安装系统Provider
6、AMS.systemReady()准备完成
1、AMS是如何启动的
/**
* Starts the small tangle of critical services that are needed to get the system off the
* ground. These services have complex mutual dependencies which is why we initialize them all
* in one place here. Unless your service is also entwined in these dependencies, it should be
* initialized in one of the other functions.
*/
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
t.traceBegin("startBootstrapServices");
...
// Wait for installd to finish starting up so that it has a chance to
// create critical directories such as /data/user with the appropriate
// permissions. We need this to complete before we initialize other services.
t.traceBegin("StartInstaller");
Installer installer = mSystemServiceManager.startService(Installer.class);
t.traceEnd();
...
t.traceBegin("StartIStatsService");
startIStatsService();
t.traceEnd();
// Start MemtrackProxyService before ActivityManager, so that early calls
// to Memtrack::getMemory() don't fail.
t.traceBegin("MemtrackProxyService");
startMemtrackProxyService();
t.traceEnd();
// Activity manager runs the show.
t.traceBegin("StartActivityManager");
// TODO: Might need to move after migration to WM.
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mWindowManagerGlobalLock = atm.getGlobalLock();
t.traceEnd();
AMS是在SystemServer进程的startBootstrapServices方法中启动。除了AMS,startBootstrapServices方法还启动了如下引导服务:
- Installd(native的安装服务)
- PowerStatsService电源状态服务
- MemtrackProxyService内存跟踪代理服务
- PowerManagerService电源管理服务
- PackageManagerService包管理服务
- 其他引导服务
2、AMS的构造方法都做了什么
首先来看看AMS的构造函数的源码实现。
public ActivityManagerService(Context systemContext, ActivityTaskManagerService atm) {
//...
//创建名为"ActivityManager"的前台线程,并获取mHandler
mHandlerThread = new ServiceThread(TAG, THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
mHandlerThread.start();
mHandler = new MainHandler(mHandlerThread.getLooper());
//通过UiThread类,创建名为"android.ui"的线程
mUiHandler = mInjector.getUiHandler(this);
//创建名为"ActivityManager:procStart"的前台线程,并获取mProcStartHandler
mProcStartHandlerThread = new ServiceThread(TAG + ":procStart", THREAD_PRIORITY_FOREGROUND, false /* allowIo */);
mProcStartHandlerThread.start();
mProcStartHandler = new Handler(mProcStartHandlerThread.getLooper());
//初始化进程列表
mProcessList.init
//实例化低内存检查者
mLowMemDetector=new LowMemDetector(this);
//实例化进程回收优先级
mOomAdjuster = new OomAdjuster(this, mProcessList, activeUids);
//-----------广播相关初始化---------------//
final BroadcastConstants foreConstants
//前台广播超时时间
foreConstants.TIMEOUT = BROADCAST_FG_TIMEOUT;
final BroadcastConstants backConstants
//后台广播超时时间
backConstants.TIMEOUT = BROADCAST_BG_TIMEOUT;
final BroadcastConstants offloadConstants
//卸载广播超时时间?
offloadConstants.TIMEOUT = BROADCAST_BG_TIMEOUT;
//实例化前台、后台和卸载广播队列,注意传入的是ActivityManager服务线程的Handler实例
mFgBroadcastQueue = new BroadcastQueue(this, mHandler,
"foreground", foreConstants, false);
mBgBroadcastQueue = new BroadcastQueue(this, mHandler,
"background", backConstants, true);
mOffloadBroadcastQueue = new BroadcastQueue(this, mHandler,
"offload", offloadConstants, true);
//创建广播队列数组
mBroadcastQueues[0] = mFgBroadcastQueue;
mBroadcastQueues[1] = mBgBroadcastQueue;
mBroadcastQueues[2] = mOffloadBroadcastQueue;
//-----------Service,Provder和App异常处理的类实例化---------------//
mServices = new ActiveServices(this);
mCpHelper = new ContentProviderHelper(this, true);
mPackageWatchdog = PackageWatchdog.getInstance(mUiContext);
mAppErrors = new AppErrors(mUiContext, this, mPackageWatchdog);
mUidObserverController = new UidObserverController(mUiHandler);
//创建/data/system目录
final File systemDir = SystemServiceManager.ensureSystemDir();
//---------猜应该是电池状态服务,根据电池状态去调整内存回收的策略-----//
mBatteryStatsService = new BatteryStatsService(systemContext, systemDir,BackgroundThread.get().getHandler());
mBatteryStatsService.getActiveStatistics().readLocked();
mBatteryStatsService.scheduleWriteToDisk();
mOnBattery = DEBUG_POWER ? true
: mBatteryStatsService.getActiveStatistics().getIsOnBattery();
mBatteryStatsService.getActiveStatistics().setCallback(this);
mOomAdjProfiler.batteryPowerChanged(mOnBattery);
//创建进程状态服务
mProcessStats = new ProcessStatsService(this, new File(systemDir, "procstats"));
//获取AppOps服务实例,用于控制一些APP行为
mAppOpsService = mInjector.getAppOpsService(new File(systemDir, "appops.xml"), mHandler);
//猜应该是Uri获取有关的一个管理类
mUgmInternal = LocalServices.getService(UriGrantsManagerInternal.class);
//实例化用户管理实例,应该和多用户管理有关
mUserController = new UserController(this);
//PendingIntent管理器
mPendingIntentController = new PendingIntentController(mHandlerThread.getLooper(), mUserController, mConstants);
//Intent防火墙,先这么理解吧
mIntentFirewall = new IntentFirewall(new IntentFirewallInterface(), mHandler);
//ATM实例初始化
mActivityTaskManager = atm;
mActivityTaskManager.initialize(mIntentFirewall, mPendingIntentController, DisplayThread.get().getLooper());
mAtmInternal = LocalServices.getService(ActivityTaskManagerInternal.class);
//添加到Watchdog监控
Watchdog.getInstance().addMonitor(this);
Watchdog.getInstance().addThread(mHandler);
//更新oomadj状态
updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_NONE);
AMS的构造方法主要做了:
1、创建 ActivityManager 、android.ui、ActivityManager:procStart 服务线程。
2、设置前台广播、后台广播的超时时间,初始化广播队列和广播数组。
3、初始化 Service, Provider,以及AppErrors。
4、创建/data/system目录,并保存procstats、appops.xml等文件。
5、创建 Intent 防火墙IntentFirewall。
6、Watchdog 实例,并将AMS 添加到看门狗Monitor中。
7、更新 oomadj 状态。
3、AMS.start()方法实现
先来看下AMS的start()方法源码。
private void start() {
//清除所有进程组
removeAllProcessGroups();
//启动/发布电源状态服务
mBatteryStatsService.publish();
//启动/发布AppOps行为管理服务
mAppOpsService.publish();
Slog.d("AppOps", "AppOpsService published");
//添加ActivityManagerInternal这个本地服务到本地服务中
LocalServices.addService(ActivityManagerInternal.class, mInternal);
LocalManagerRegistry.addManager(ActivityManagerLocal.class,(ActivityManagerLocal) mInternal);
mActivityTaskManager.onActivityManagerInternalAdded(</