一、 AcitivityManager getRunningAppProcesses过程:
二、 AMS的Binder结构
三、 相关文件:
android.app.IActivityManager.java
android.app.ActivityManager.aidl
android.app.ActivityManager.java
android.app.ActivityManagerInternal.java
android.app.ActivityManagerNative.java
android.app.ActivityThread.java
com.android.server.am.ActivityManagerService.java
com.android.server.am.ActivityRecord.java
四、 分析
zygote entry :
/**- The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
- The main entry point from zygote.
factory check :
public SystemServer() {
// Check for factory test mode.
private final int mFactoryTestMode = FactoryTest.getMode();
}在systemserver主线程中设置系统属性、加载android_servers库、启动各种java服务
private void run() {
// Prepare the main looper thread (this thread).
Looper.prepareMainLooper();// Initialize native services. System.loadLibrary("android_servers"); // Initialize the system context. createSystemContext(); /*初始化系统上下文对象mSystemContext,并设置默认的主题,mSystemContext实际上是一个ContextImpl对象。 *调用ActivityThread.systemMain()的时候,会调用ActivityThread.attach(true),而在attach()里面, *则创建了Application对象,并调用了Application.onCreate()。 */ private void createSystemContext() { ActivityThread activityThread = ActivityThread.systemMain(); mSystemContext = activityThread.getSystemContext(); mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar); } // Create the system service manager. mSystemServiceManager = new SystemServiceManager(mSystemContext); LocalServices.addService(SystemServiceManager.class, mSystemServiceManager); // Start services. try { startBootstrapServices(); startCoreServices(); startOtherServices(); } catch (Throwable ex) { Slog.e("System", "******************************************"); Slog.e("System", "************ Failure starting system services", ex); throw ex; } // Loop forever. Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited");
}
启动AMS
/**
* 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. private ActivityManagerService mActivityManagerService = mSystemServiceManager.startService( ActivityManagerService.Lifecycle.class).getService(); { 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() { mService.start(); private void start() { Process.removeAllProcessGroups(); mProcessCpuThread.start(); mBatteryStatsService.publish(mContext); mAppOpsService.publish(mContext); Slog.d("AppOps", "AppOpsService published"); LocalServices.addService(ActivityManagerInternal.class, new LocalService()); } } public ActivityManagerService getService() { return mService; } } } mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); // Now that the power manager has been started, let the activity manager // initialize power management features. mActivityManagerService.initPowerManagement(); // Set up the Application instance for the system process and get started. mActivityManagerService.setSystemProcess();
}
/**
- Starts some essential services that are not tangled up in the bootstrap process.
*/
private void startCoreServices() {
mActivityManagerService.setUsageStatsManager(
LocalServices.getService(UsageStatsManagerInternal.class));
}
/**
- Starts a miscellaneous grab bag of stuff that has yet to be refactored
and organized.
*/
private void startOtherServices() {Slog.i(TAG, "System Content Providers"); mActivityManagerService.installSystemProviders(); Slog.i(TAG, "Init Watchdog"); final Watchdog watchdog = Watchdog.getInstance(); watchdog.init(context, mActivityManagerService); Slog.i(TAG, "Window Manager"); wm = WindowManagerService.main(context, inputManager, mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL, !mFirstBoot, mOnlyCore); ServiceManager.addService(Context.WINDOW_SERVICE, wm); ServiceManager.addService(Context.INPUT_SERVICE, inputManager); mActivityManagerService.setWindowManager(wm); mActivityManagerService.systemReady(new Runnable() { try { startSystemUi(context); static final void startSystemUi(Context context) { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.android.systemui", "com.android.systemui.SystemUIService")); //Slog.d(TAG, "Starting service: " + intent); context.startServiceAsUser(intent, UserHandle.OWNER); } } catch (Throwable e) { reportWtf("starting System UI", e); } }
}
- Starts some essential services that are not tangled up in the bootstrap process.