ActivityManagerService简称AMS,单单这个类本身就超过2万7千行代码,涉及到的类加起来更是一个天文数字,虽然不像前面的启动流程都是底层C和C++代码,但是如果不是做这一块的开发的话还是主要了解为主,扩展一下知识面。
AMS是什么?
1.从java角度来看,ams就是一个java对象,实现了Ibinder接口,所以它是一个用于进程之间通信的
接口,这个对象初始化是在systemServer.java 的run()方法里面
2. AMS是一个服务
ActivityManagerService从名字就可以看出,它是一个服务,用来管理Activity,而且是一个系统服务,
就是包管理服务,电池管理服务,震动管理服务等。
3. AMS是一个Binder
ams实现了Ibinder接口,所以它是一个Binder,这意味着他不但可以用于进程间通信,还是一个线程,因为一个Binder就是一个线程。
如果我们启动一个hello World安卓用于程序,里面不另外启动其他线程,这个里面最少要启动4个线程
1 main线程,只是程序的主线程,也是日常用到的最多的线程,也叫UI线程,因为android的组件是非线程安全的,所以只允许UI/MAIN线程来操作。
2 GC线程,java有垃圾回收机制,每个java程序都有一个专门负责垃圾回收的线程,
3 Binder1 就是我们的ApplicationThread,这个类实现了Ibinder接口,用于进程之间通信,具体来说,就是我们程序和AMS通信的工具
4 Binder2 就是我们的ViewRoot.W对象,他也是实现了IBinder接口,就是用于我们的应用程序和wms通信的工具。wms就是WindowManagerServicer ,和ams差不多的概念,不过他是管理窗口的系统服务。
AMS伴随着一些列系统服务的启动而启动,系统服务进程首先createSystemContext
System资源加载,创建context
加载完各种资源之后就开始启动各种各样的服务了,分为上面截图中的三个方法。
t.traceBegin("StartInstaller");
Installer installer = mSystemServiceManager.startService(Installer.class);
t.traceEnd();
上面代码是系统log和创建一个安装器Installer
下面又找到了启动AMS的代码:
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
后面的代码中很多地方都要关联到AMS,所以它很早就启动了。
创建AMS通过mSystemServiceManager,它是一个系统隐藏类,外面是调不到的。
mSystemServiceManager:专门管理各种服务活动(管理SystemtService启动的80多种服务,java层)
service_manager:binder说的SM,管理C、C++C层,两者是不同的对象
传递的是AMS里面的一个静态内部类,通过它来创建。
public static final class Lifecycle extends SystemService
@SystemApi(client = Client.SYSTEM_SERVER)
public abstract class SystemService {
/** @hide */
protected static final boolean DEBUG_USER = false;
/*
* The earliest boot phase the system send to system services on boot.
*/
public static final int PHASE_WAIT_FOR_DEFAULT_DISPLAY = 100;
/**
* After receiving this boot phase, services can obtain lock settings data.
*/
public static final int PHASE_LOCK_SETTINGS_READY = 480;
/**
* After receiving this boot phase, services can safely call into core system services
* such as the PowerManager or PackageManager.
*/
public static final int PHASE_SYSTEM_SERVICES_READY = 500;
/**
* After receiving this boot phase, services can safely call into device specific services.
*/
public static final int PHASE_DEVICE_SPECIFIC_SERVICES_READY = 520;
/**
* After receiving this boot phase, services can broadcast Intents.
*/
public static final int PHASE_ACTIVITY_MANAGER_READY = 550;
/**
* After receiving this boot phase, services can start/bind to third party apps.
* Apps will be able to make Binder calls into services at this point.
*/
public static final int PHASE_THIRD_PARTY_APPS_CAN_START = 600;
/**
* After receiving this boot phase, services can allow user interaction with the device.
* This phase occurs when boot has completed and the home application has started.
* System services may prefer to listen to this phase rather than registering a
* broadcast receiver for {@link android.content.Intent#ACTION_LOCKED_BOOT_COMPLETED}
* to reduce overall latency.
*/
public static final int PHASE_BOOT_COMPLETED = 1000;
做了好几个常量,等下后面AMS会根据不同阶段来启动对应的服务。
它需要管理的东西非常多,包括我们android的四大组件,四大组件有分别对应的service,AMS管理他们的service。
例如:前台服务、后台服务、activityService、电量管理统计服务、进程相关统计的服务、VR相关的控制器、activityStartController、recentasks(手机按钮查看最近打开的应用)等等。
public void setSystemProcess() {
try {
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, /* allowIsolated= */ true,
DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PROTO);
ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
ServiceManager.addService("meminfo", new MemBinder(this), /* allowIsolated= */ false,
DUMP_FLAG_PRIORITY_HIGH);
ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
ServiceManager.addService("dbinfo", new DbBinder(this));
if (MONITOR_CPU_USAGE) {
ServiceManager.addService("cpuinfo", new CpuBinder(this),
/* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
}
ServiceManager.addService("permission", new PermissionController(this));
ServiceManager.addService("processinfo", new ProcessInfoService(this));
ServiceManager.addService("cacheinfo", new CacheBinder(this));
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(
"android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
通过setSystemProcess方法添加这些服务到SM进行管理他们的信息传输,通过binder,他们都实现了binder。
包括去设置字体管理、开发者选项性能统计等等我们开发人员常见的各种设置,所以说AMS非常重要。
包括跟windowManager相关联等等很多都是通过AMS去执行的。
上面一系列服务启动可以说是初始化,最后执行startOtherService
通过最后一步startHomeActivityLocked启动我们的launcher,这个时候我们差不多就可以看到桌面了。
回顾一下启动的流程:
init进程:用户态的第一个进程,这个时候主要还是加载系统的资源,通过解析init.rc来创建各种服务进程,但是它几乎不包含我们android的framework资源,所以就创建了一个叫Zygote进程
Zygote进程:有了一个进程所需要的必要资源,通过preload加载class资源和vm等。
app:通过fork上面Zygote进程来的。
SystemService进程:Zygote进程会fork一个关键进程SystemService,它会创建大量服务,加载AndroidFrameWork所需要的大量资源,创建大量的服务,有可能创建的服务是进程级别不是线程级别。