AMS is introduced
ActivityManagerService AMS for short, is one of the core features in the Android kernel, by com. Android. Server SystemService. Java startup.
AMS Startup process
Because the following process involves too much source code, I use UML flow chart and code screenshots here to show
Android Startup
Starting an application process
ServiceManager start
AMS registration
AMS startup details
Code flow:
-
AMS is launched in SystemService into, mainly in the com/android/server/SystemServer. Java main () function.
/** * The main function here is mainly called by zygote via reflection */ public static void main(String[] args) { new SystemServer().run(); } Copy the code
-
Can be learned from above, the main method is mainly perform com/android/server/SystemServer. Java run () function, we specifically look at the run () really do?
private void run(a) { try{...// The dynamic library libandroid_servers.so is loaded System.loadLibrary("android_servers"); // Create SystemServiceManager, which creates, starts, and manages the life cycle of system services mSystemServiceManager = new SystemServiceManager(mSystemContext); mSystemServiceManager.setRuntimeRestarted(mRuntimeRestart); LocalServices.addService(SystemServiceManager.class, mSystemServiceManager); } finally{}// Start various services try { traceBeginAndSlog("StartServices"); ActivityManagerService, PowerManagerService, and PackageManagerService are started startBootstrapServices(); BatteryService, UsageStatsService, and WebViewUpdateService services are enabled startCoreServices(); CameraService, AlarmManagerService, and VrManagerService are startedstartOtherServices(); SystemServerInitThreadPool.shutdown(); . }catch (Throwable ex) { throw ex; } finally { traceEnd(); } // Loop forever. Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); } Copy the code
The core task of the run () function is to initialize some transactions and the startup of the core service. Here there are more than 80 core services at the beginning. The system roughly divides the core services into three categories, namely boot service, core service and other services, because this section mainly studies the startup of AMS. So let’s just focus on startBootstrapServices(); Content will do.
-
The main function of startBoostrapService() is officially to start small critical services that need to be obtained. These services have complex interdependencies, which is why we initialized them all here. Unless your service is also entangled with these dependencies, you should initialize it in another function.
public void startBootstrapServices(a){...// Start AMS service mActivityManagerService = mSystemServiceManager.startService( ActivityManagerService.Lifecycle.class).getService(); // Set the manager mActivityManagerService.setSystemServiceManager(mSystemServiceManager); // Set the installermActivityManagerService.setInstaller(installer); . }Copy the code
The SystemServiceManager startService method is called. Method of parameter is ActivityManagerService. Lifecycle. Class com/android/server/SystemServiceManager. Java we take a look at the SSM startService What does the method do?
-
SSM startService(Class serviceClass)
public <T extends SystemService> T startService(Class<T> serviceClass) { try{...final T service; try { Constructor<T> constructor = serviceClass.getConstructor(Context.class); // instantiate the object service = constructor.newInstance(mContext); } catch (InvocationTargetException ex) { throw new RuntimeException("Failed to create service " + name + ": service constructor threw an exception", ex); } startService(service); return service; } finally{ Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); }}Copy the code
Tracing the code will tell you that Lifecycle is passed to the startService method as an argument that Lifecycle inherits from SystemService. First, create an instance of Lifecycle via reflection and get the constructor passed in for Lifecycle, This is followed by calling the Constructor newInstance method to create a service object of type Lifecycle. Then add the newly created Service to an mServices object of type ArrayList to complete the registration. Finally, the onStart method is called to start the service and return the service. Lifecycle is an internal class of AMS.
-
Lifecycle to
public static final class Lifecycle extends SystemService { private final ActivityManagerService mService; public Lifecycle(Context context) { super(context); mService = new ActivityManagerService(context); } @Override public void onStart(a) { mService.start(); } public ActivityManagerService getService(a) { returnmService; }}Copy the code
Lifecyle onStart()
public void startService(@NonNull final SystemService service) { // Add AMS service to system service mServices.add(service); // Start it. long time = System.currentTimeMillis(); try { // Execute the Lifecyle onStart function method service.onStart(); } catch (RuntimeException ex) { throw new RuntimeException("Failed to start service " + service.getClass().getName() + ": onStart threw an exception", ex); } warnIfTooLong(System.currentTimeMillis() - time, service, "onStart"); } Copy the code
The fourth step above has instantiated Lifecycle and created new using the constructor newInstance method
ActivityManagerService(context); Object, followed by a call to service.start() in step 4 startService(service); You actually call the Lifecyle onStart function method.
-
ActivityManagerService getService()
mActivityManagerService = mSystemServiceManager.startService( ActivityManagerService.Lifecycle.class).getService(); Copy the code
From the code above we know that an AMS instance will be created by calling getService in AMS’s inner class Lifecycle.
-
The final startup of the AMS process is performed in Zygote, as shown in the flow chart of the application process.