AMS-ATMS 启动/初始化
eaaomk 2021-12-25 23:00:00 JavaAndroid
# AMS-ATMS 启动/初始化
AMS 的启动流程代码展示:
- 调用startBootstrapServices,开启服务
//frameworks/base/services/java/com/android/server/SystemServer.java
private void run() {
...
// Start services.
try {
t.traceBegin("StartServices");
// 在startBootstrapServices里面开启AMS服务
startBootstrapServices(t);
startCoreServices(t);
// WMS在这里启动,启动之后会将WMS放入到AMS以及ATMS中
startOtherServices(t);
checkAccessGranted(mSystemContext);
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
t.traceEnd(); // StartServices
}
...
}
- ATMS ,是Android核心服务,在Android 新版本中,activity的调度和管理已经从AMS移到了ActivityTaskManagerService中,ATMS 调度的不仅仅是Activity,还包括其容器(task、stacks、displays等),AMS负责四大组件中另外3个(service, broadcast, contentprovider)的管理和调度
- ATMS并不是直接启动,而是通过 Lifecycle 来间接实现,Lifecycle 是 ATMS 里的静态内部类,静态内部类不依赖于外部类,不会隐式持有外部类的引用,Lifecycle 构造方法调用时,会初始化内部成员变量 mService,即调用ATMS的构造方法,构造方法中会执行一些初始化操作.
//frameworks/base/services/java/com/android/server/SystemServer.java
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
...
t.traceBegin("StartActivityManager");
// 启动ATMS
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
// 启动AMS,ATMS被放入到了AMS中
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mWindowManagerGlobalLock = atm.getGlobalLock();
t.traceEnd();
...
}
- AMS 与ATMS 的启动方法十分相似,都是调用了ssm中的startService 方法,通过反射的方式初始化.我们可以发现,实际上AMS 初始化的是AMSE,它是继承AMS类的,ATMS被放在了AMS中保存起来.
//frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public Lifecycle(Context context) {
super(context);
// AMTS内部Lifecycle类的构造方法
mService = new ActivityTaskManagerService(context);
// AMS内部Lifecycle类的构造方法
mService = new ActivityManagerServiceEx(context, sAtm);
}
- AMS 初始化部分重要细节
//frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public ActivityManagerService(Context systemContext, ActivityTaskManagerService atm) {
...
mContext = systemContext;
...
mSystemThread = ActivityThread.currentActivityThread();
mUiContext = mSystemThread.getSystemUiContext();
//AMS 处理消息的Handler
mHandlerThread = new ServiceThread(TAG,
THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
mHandlerThread.start();
mHandler = new MainHandler(mHandlerThread.getLooper());
//UI handler
mUiHandler = mInjector.getUiHandler(this);
....
//初始化3种广播队列,前台,后台 offload
mFgBroadcastQueue = new BroadcastQueue(this, mHandler,
"foreground", foreConstants, false);
mBgBroadcastQueue = new BroadcastQueue(this, mHandler,
"background", backConstants, true);
mOffloadBroadcastQueue = new BroadcastQueue(this, mHandler,
"offload", offloadConstants, true);
...
mServices = new ActiveServicesEx(this);
...
//初始化ATMS
mActivityTaskManager = atm;
//在此方法中,初始化了ActivityStackSupervisor,它是用来管理所有activity的.
mActivityTaskManager.initialize(mIntentFirewall, mPendingIntentController,
DisplayThread.get().getLooper());
//CPU监控器了~开启一个线程循环检测CPU使用状态
mProcessCpuThread = new Thread("CpuTracker") {
@Override
public void run() {
...
}
};
// 加入看门狗
Watchdog.getInstance().addMonitor(this);
Watchdog.getInstance().addThread(mHandler);
}
- ATMS 初始化做的那些事
//frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public ActivityManagerService(Context systemContext, ActivityTaskManagerService atm) {
...
//将生成的ATMS 赋值给AMS的变量mActivityTaskManager,随后调用ATMS的initialize函数进行初始化
mActivityTaskManager = atm;
mActivityTaskManager.initialize(mIntentFirewall, mPendingIntentController,
...
}
//frameworks/base/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
public void initialize(IntentFirewall intentFirewall, PendingIntentController intentController,
Looper looper) {
//初始化了两个handler去处理一些事物
mH = new H(looper);
mUiHandler = new UiHandler();
...
// 注意下这里,实质上Activity 的调度和管理由mStackSupervisor处理
mStackSupervisor = createStackSupervisor();
// mTaskChangeNotificationController内部是一个handler,用来做task相关变化需要处理的事物
mTaskChangeNotificationController =
new TaskChangeNotificationController(mGlobalLock, mStackSupervisor, mH);
//初始化最近任务栈,也将mStackSupervisor中的RecentTasks进行了设置
setRecentTasks(new RecentTasks(this, mStackSupervisor));
...
}
protected ActivityStackSupervisor createStackSupervisor() {
final ActivityStackSupervisor supervisor = new ActivityStackSupervisor(this, mH.getLooper());
// 这里调用了supervisor的初始化
supervisor.initialize();
return supervisor;
}
- ActivityStackSupervisor 初始化
//frameworks/base/services/core/java/com/android/server/wm/ActivityStackSupervisor.java
public void initialize() {
// 确保supervisor只会初始化一次
if (mInitialized) {
return;
}
mInitialized = true;
// 初始化RunningTasks,里面包含了所有正在运行的tasks.
setRunningTasks(new RunningTasks());
...
}