一、 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.