ActivityManagerService 启动流程
它是在SyetemServer
进程中启动的:
文件所在目录: frameworks/base/services/java/com/android/server/SystemServer.java
private void run() {
Looper.prepareMainLooper();
// Initialize native services.
System.loadLibrary("android_servers");
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
// Start services.
// 引导服务
startBootstrapServices();
// 核心服务
startCoreServices();
// 其他服务
startOtherServices();
// Loop forever.
Looper.loop();
}
SyetemServer
启动了一堆服务,这些服务由 mSystemServiceManager
进行创建、启动和生命周期管理。
ActivityManagerService
则由此创建:
/**
* 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() {
// 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.
Installer installer = mSystemServiceManager.startService(Installer.class);
// Activity manager runs the show.
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
// Set up the Application instance for the system process and get started.
mActivityManagerService.setSystemProcess();
// The sensor service needs access to package manager service, app ops
// service, and permissions service, therefore we start it after them.
startSensorService();
}
注意1:mSystemServiceManager
的startService(Class
,包含了创建,注册,启动service的过程:
/**
* Creates and starts a system service. The class must be a subclass of
* {@link com.android.server.SystemService}.
*
* @param serviceClass A Java class that implements the SystemService interface.
* @return The service instance, never null.
* @throws RuntimeException if the service fails to start.
*/
@SuppressWarnings("unchecked")
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 {
Constructor constructor = serviceClass.getConstructor(Context.class);
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);
}
// Register it.
mServices.add(service);
// Start it.
try {
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + name
+ ": onStart threw an exception", ex);
}
return service;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
}
}
注意2:mActivityManagerService.setSystemProcess()
:
public void setSystemProcess() {
try {
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
ServiceManager.addService("meminfo", new MemBinder(this));
ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
ServiceManager.addService("dbinfo", new DbBinder(this));
if (MONITOR_CPU_USAGE) {
ServiceManager.addService("cpuinfo", new CpuBinder(this));
}
ServiceManager.addService("permission", new PermissionController(this));
ServiceManager.addService("processinfo", new ProcessInfoService(this));
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(
"android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(
"Unable to find android system package", e);
}
}
Activity 启动流程
PlantUML 文件记录
- Activity 成长路程
@startuml Activity 成长路程
Activity->Activity:startActivity
Activity->Activity:startActivityForResult
Activity->Activity:startActivityForResult
Activity->Instrumentation:execStartActivity
Instrumentation->ActivityManagerService:startActivity
ActivityManagerService->ActivityManagerService:startActivityAsUser
ActivityManagerService->ActivityStarter:startActivityMayWait
ActivityStarter->ActivityStarter:startActivityLocked
ActivityStarter->ActivityStarter:startActivityUnchecked
ActivityStarter->ActivityStack:startActivityLocked
ActivityStarter->ActivityStackSupervisor:resumeFocusedStackTopActivityLocked
ActivityStackSupervisor->ActivityStack:resumeTopActivityUncheckedLocked
ActivityStack->ActivityStack:resumeTopActivityInnerLocked
ActivityStack->ActivityStackSupervisor:startSpecificActivityLocked
ActivityStackSupervisor->ActivityStackSupervisor:realStartActivityLocked
ActivityStackSupervisor->ApplicationThread:scheduleLaunchActivity
ApplicationThread->ActivityThread:sendMessage(H.LAUNCH_ACTIVITY)
ActivityThread->ActivityThread:handleLaunchActivity
ActivityThread->ActivityThread:performLaunchActivity
ActivityThread->Instrumentation:callActivityOnCreate
Instrumentation->Instrumentation:prePerformCreate
Instrumentation->Activity:performCreate
Activity->Activity:onCreate
ActivityThread->ActivityThread:handleResumeActivity
ActivityThread->ActivityThread:performResumeActivity
ActivityThread->Activity:performResume
Activity->Activity:performRestart
Activity->Instrumentation:callActivityOnRestart
Instrumentation->Activity:onRestart
Activity->Instrumentation:callActivityOnResume
Instrumentation->Activity:onResume
ActivityThread->Activity:makeVisibale
@enduml