本文均采用Android 13代码进行讲解,学习可以使用以下地址:Search
AMS的启动是在SyetemServer进程中启动的,从SyetemServer的main方法开始进入:
frameworks/base/services/java/com/android/server/SystemServer.java
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
main方法中只调用了SystemServer的run方法,如下所示。
从下面的注释中可以看到,官方把系统服务分为了三种类型,分别是引导服务、核心服务和其他服务,其中其他服务是一些非紧要和一些不需要立即启动的服务。系统服务总共大约有80多个,我们主要来查看引导服务AMS是如何启动的。
private void run() {
// Initialize native services.
//1.加载了动态库libandroid_servers.so
System.loadLibrary("android_servers");
......
// Create the system service manager.
//2.创建SystemServiceManager,它会对系统的服务进行创建、启动和生命周期管理。
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setStartInfo(mRuntimeRestart,
mRuntimeStartElapsedTime, mRuntimeStartUptime);
mDumper.addDumpable(mSystemServiceManager);
......
// Start services.
try {
t.traceBegin("StartServices");
//用SystemServiceManager启动了ActivityManagerService、PowerManagerService、PackageManagerService等服务。
startBootstrapServices(t);
//启动了BatteryService、UsageStatsService和WebViewUpdateService。
startCoreServices(t);
//启动了CameraService、AlarmManagerService、VrManagerService等服务。这些服务的父类均为SystemService。
startOtherServices(t);
startApexServices(t);
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
t.traceEnd(); // StartServices
}
...
}
调用了ActivityManagerService.Lifecycle.startService方法
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
t.traceBegin("startBootstrapServices");
//尽早启动watchdog,这样我们就可以使系统服务器崩溃
// 如果我们在早期启动时死锁
t.traceBegin("StartWatchdog");
final Watchdog watchdog = Watchdog.getInstance();
watchdog.start();
mDumper.addDumpable(watchdog);
.....
// 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();
//调用了ActivityManagerService.Lifecycle.startService方法,跟进代码看最终调用哪个方法
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mWindowManagerGlobalLock = atm.getGlobalLock();
t.traceEnd();
......
/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
最终调用SystemServiceManager的startService方法,参数就是ActivityManagerService.Lifecycle.class,继续跟进SystemServiceManager,最终调用Lifecycle的getService方法,这个方法会返回AMS类型的mService对象,这样AMS实例就会被创建并且返回。
//Lifecycle继承自SystemService
public static final class Lifecycle extends SystemService {
public Lifecycle(Context context) {
super(context);
//当通过反射来创建Lifecycle实例时,会调用此方法创建AMS实例
mService = new ActivityManagerService(context, sAtm);
}
public static ActivityManagerService startService(
SystemServiceManager ssm, ActivityTaskManagerService atm) {
sAtm = atm;
//调用SystemServiceManager的startService方法,参数就是ActivityManagerService.Lifecycle.class,最终调用Lifecycle的getService方法,这个方法会返回AMS类型的mService对象,这样AMS实例就会被创建并且返回。
return ssm.startService(ActivityManagerService.Lifecycle.class).getService();
@Override
public void onStart(){
Service.start;
}
...
//这个方法会返回AMS类型的mService对象,这样AMS实例就会被创建并且返回。
public ActivityManagerService getService() {
return mService;
}
}
/frameworks/base/services/core/java/com/android/server/SystemServiceManager.java
首先会获取传进来的Lifecycle的构造器constructor,然后调用constructor的newInstance方法来创建Lifecycle类型的service对象,调用重载的startService,最后返回service
public T startService(Class serviceClass) {
try {
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);
// Create the service.
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
+ ": service must extend " + SystemService.class.getName());
}
final T service;
try {
//1.获取传进来的Lifecycle的构造器constructor
Constructor constructor = serviceClass.getConstructor(Context.class);
//调用constructor的newInstance方法来创建Lifecycle类型的service对象
service = constructor.newInstance(mContext);
} catch (InstantiationException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (InvocationTargetException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service constructor threw an exception", ex);
}
//调用重载的startService
startService(service);
//返回service
return service;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
}
首先会将上面创建的service添加到ArrayList类型的mServices对象中来完成注册,然后调用service的onStart方法来启动service
public void startService(@NonNull final SystemService service) {
// Check if already started
String className = service.getClass().getName();
if (mServiceClassnames.contains(className)) {
Slog.i(TAG, "Not starting an already started service " + className);
return;
}
mServiceClassnames.add(className);
//1.将上面创建的service添加到ArrayList类型的mServices对象中来完成注册
mServices.add(service);
// Start it.
long time = SystemClock.elapsedRealtime();
try {
//2.调用service的onStart方法来启动service
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + service.getClass().getName()
+ ": onStart threw an exception", ex);
}
warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
}
总的流程主要涉及SystemServer、ActivityManagerService、SystemServiceManager三个类,SystemServer的run方法会调用去启动引导服务startBootstrapServices,其中AMS也在里面,startBootstrapServices调用了startService方法,跟进代码看最终调用SystemServiceManager里面的startService,中间会有一系列的调用,但最后会通过Lifecycle创建AMS实例,然后通过Lifecycle里面的getService(),这个方法会返回AMS类型的mService对象。