AOSP-系统服务生命周期

我们知道像系统中的AMS,WMS,PKMS等这些系统服务均是通过ServiceManager.addService("xxx", new XXManagerService())将自己的Binder Stub注册进入SM才能够让其他进程利用Binder与之通信。

如果自定义系统服务如果需要根据系统启动的不同阶段进行不同的处理则需要注册生命周期回调,需要通过
mSystemServiceManager.startService 来完成生命周期的注册。下面就以AMS的启动进行浅析。

SystemServer中的启动入口

/frameworks/base/services/java/com/android/server/SystemServer中的源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
t.traceBegin("startBootstrapServices");
// 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();
//......
// Set up the Application instance for the system process and get started.
t.traceBegin("SetSystemProcess");
//注册AMS服务
mActivityManagerService.setSystemProcess();
t.traceEnd();
//......
t.traceEnd(); // startBootstrapServices
}

其实mActivityManagerService.setSystemProcess()就是我们所熟知的注册Binder的实现了,看源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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));
mAppProfiler.setCpuInfoService();
ServiceManager.addService("permission", new PermissionController(this));
ServiceManager.addService("processinfo", new ProcessInfoService(this));
ServiceManager.addService("cacheinfo", new CacheBinder(this));
//......
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(
"Unable to find android system package", e);
}
//......
}

服务生命周期监听

上文中我们看到在setSystemProcess前有一行为:

mActivityManagerService = ActivityManagerService.Lifecycle.startService(mSystemServiceManager, atm);

这就是接下来要说的为服务注册的生命周期监听。

ActivityManagerService.Lifecycle就相当于生命周期的回调接口对象,它继承自

/frameworks/base/services/core/java/com/android/server/SystemService

SystemService中主要需要实现两个方法:

  • onStart() :mSystemServiceManager.startService第一时间回调该函数。

  • onBootPhase(int phase) : 系统启动的各个阶段会回调该函数

    • SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY

      这是一个依赖项,只有DisplayManagerService中进行了对应处理;

    • SystemService.PHASE_LOCK_SETTINGS_READY

      经过这个引导阶段后,服务才可以接收到wakelock相关设置数据;

    • SystemService.PHASE_SYSTEM_SERVICES_READY

      经过这个引导阶段 后,服务才可以安全地使用核心系统服务

    • SystemService.PHASE_ACTIVITY_MANAGER_READY

      经过这个引导阶 段后,服务可以发送广播

    • SystemService.PHASE_THIRD_PARTY_APPS_CAN_START

      经过这个引导阶段后,服务可以启动第三方应用,第三方应用也可以通过Binder来调用服务。

    • SystemService.PHASE_BOOT_COMPLETED

      经过这个引导阶段后,说明服务启动完成,这时用户就可以和设备进行交互。

我们看下AMS中的Lifecycle实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
private static ActivityTaskManagerService sAtm;
public Lifecycle(Context context) {
super(context);
mService = new ActivityManagerService(context, sAtm);
}
public static ActivityManagerService startService(
SystemServiceManager ssm, ActivityTaskManagerService atm) {
sAtm = atm;
return ssm.startService(ActivityManagerService.Lifecycle.class).getService();
}
@Override
public void onStart() {
mService.start();
}
@Override
public void onBootPhase(int phase) {
mService.mBootPhase = phase;
if (phase == PHASE_SYSTEM_SERVICES_READY) {
mService.mBatteryStatsService.systemServicesReady();
mService.mServices.systemServicesReady();
} else if (phase == PHASE_ACTIVITY_MANAGER_READY) {
mService.startBroadcastObservers();
} else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
mService.mPackageWatchdog.onPackagesReady();
}
}
@Override
public void onUserStopped(@NonNull TargetUser user) {
mService.mBatteryStatsService.onCleanupUser(user.getUserIdentifier());
}
public ActivityManagerService getService() {
return mService;
}
}

比如在AMS中在 PHASE_ACTIVITY_MANAGER_READY 为广播做准备工作 ,经过这个阶段后,也就是需要在下一个阶段 PHASE_THIRD_PARTY_APPS_CAN_START 才可以发送广播。