ActivityManager主要功能有:
①启动或杀死应用程序进程。
②启动调度Activyty生命周期。
③注册Broadcast Receiver,接收分发Broadcast。
④启动发布Content Provocer。
⑤task相关。
⑥OOM。
ActivityManagerService是在SystemServer启动过程中启动的,代码如下。
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.
//①启动Liifcycle,进而启动ActivityManagerService
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.
//②调用setSystemProcess()方法。
mActivityManagerService.setSystemProcess();
……
private void startOtherServices() {
final Context context = mSystemContext;
AccountManagerService accountManager = null;
ContentService contentService = null;
VibratorService vibrator = null;
……
Slog.i(TAG, "System Content Providers");
//③调用installSystemProviders方法。
mActivityManagerService.installSystemProviders();
……
//④调用systemReday方法
mActivityManagerService.systemReady(new Runnable() {
@Override
public void run() {
Slog.i(TAG, "Making services ready");
mSystemServiceManager.startBootPhase(
SystemService.PHASE_ACTIVITY_MANAGER_READY);
try {
mActivityManagerService.startObservingNativeCrashes();
} catch (Throwable e) {
reportWtf("observing native crashes", e);
}
Slog.i(TAG, "WebViewFactory preparation");
WebViewFactory.prepareWebViewInSystemServer();
try {
startSystemUi(context);
} catch (Throwable e) {
reportWtf("starting System UI", e);
}
try {
if (networkScoreF != null) networkScoreF.systemReady();
} catch (Throwable e) {
reportWtf("making Network Score Service ready", e);
}
下面详细分析每个步骤。
1.第一阶段,启动ActivityManagerService。
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
//通过反射获取到会启动ActivityManagerService构造方法。
}
public ActivityManagerService(Context systemContext) {
mContext = systemContext;
mFactoryTest = FactoryTest.getMode();
mSystemThread = ActivityThread.currentActivityThread();