一 前言
Android 系统服务进程 system_server,进入消息循环前的最后一步,就是调用 AMS 的 systemReady() 方法。今天我们分析一下这里的流程。
二 systemReady 第一部分
ActivityManagerService.java
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
synchronized(this) {
if (mSystemReady) {
// If we're done calling all the receivers, run the next "boot phase" passed in
// by the SystemServer
if (goingCallback != null) {
goingCallback.run();
}
return;
}
mLocalDeviceIdleController
= LocalServices.getService(DeviceIdleController.LocalService.class);
mActivityTaskManager.onSystemReady();
// Make sure we have the current profile info, since it is needed for security checks.
mUserController.onSystemReady();
mAppOpsService.systemReady();
mSystemReady = true;
}
......
}
这里 mSystemReady 是 false,所以回调方法不会在这里被执行。接下来会调用一些关联类的 systemReady() 方法,然后把标志位 mSystemReady 置为 true。
ArrayList<ProcessRecord> procsToKill = null;
synchronized(mPidsSelfLocked) {
for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
ProcessRecord proc = mPidsSelfLocked.valueAt(i);
if (!isAllowedWhileBooting(proc.info)){
if (procsToKill == null) {
procsToKill = new ArrayList<ProcessRecord>();
}
procsToKill.add(proc);
}
}
}
synchronized(this) {
if (procsToKill != null) {
for (int i=procsToKill.size()-1; i>=0; i--) {
ProcessRecord proc = procsToKill.get(i);
Slog.i(TAG, "Removing system update proc: " + proc);
mProcessList.removeProcessLocked(proc, true, false, "system update done");
}
}
// Now that we have cleaned up any update processes, we
// are ready to start launching real processes and know that
// we won't trample on them any more.
mProcessesReady = true;
}
清理进程,把不允许在开机阶段运行的进程都结束掉。判断标准是:应用程序的 AM 配置文件是否有配置 android:persistent 属性。
mAtmInternal.updateTopComponentForFactoryTest();
工厂测试相关的逻辑,正常情况下这个条件不成立,所以不管它。
if (goingCallback != null) goingCallback.run();
执行 systemReady() 传入的回调方法,在 SystemServer.java 类中
三 systemReady 第二部分之回调方法
SystemServer.java
这个回调方法很长,足有 200 行,我们单独作为一节来分析。
traceBeginAndSlog("StartActivityManagerReadyPhase");
mSystemServiceManager.startBootPhase(
SystemService.PHASE_ACTIVITY_MANAGER_READY);
traceEnd();
AMS 启动完成,系统进入阶段 PHASE_ACTIVITY_MANAGER_READY,对其它服务调用 onBootPhase() 方法传出这个PHASE。
traceBeginAndSlog("StartObservingNativeCrashes");
try {
mActivityManagerService.startObservingNativeCrashes();
} catch (Throwable e) {
reportWtf("observing native crashes", e);
}
traceEnd();
启动 native crash监测。
// No dependency on Webview preparation in system server. But this should
// be completed before allowing 3rd party
final String WEBVIEW_PREPARATION = "WebViewFactoryPreparation";
Future<?> webviewPrep = null;
if (!mOnlyCore && mWebViewUpdateService != null) {
webviewPrep = SystemServerInitThreadPool.get().submit(() -> {
Slog.i(TAG, WEBVIEW_PREPARATION);
TimingsTraceLog traceLog = new TimingsTraceLog(
SYSTEM_SERVER_TIMING_ASYNC_TAG, Trace.TRACE_TAG_SYSTEM_SERVER);
traceLog.traceBegin(WEBVIEW_PREPARATION);
ConcurrentUtils.waitForFutureNoInterrupt(mZygotePreload, "Zygote preload");
mZygotePreload = null;
mWebViewUpdateService.prepareWebViewInSystemServer();
traceLog.traceEnd();
}, WEBVIEW_PREPARATION

最低0.47元/天 解锁文章
520

被折叠的 条评论
为什么被折叠?



