SystemServer进程的创建及Launcher启动

SystemServer进程的创建及Launcher启动

1.SystemServer进程的创建

ZygoteInit.main方法中里fork出个SystemServer进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* Hardcoded command line to start the system server */
String args[] = {
"--setuid=1000",
"--setgid=1000",
"--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1023,1024,1032,1065,3001,3002,3003,3006,3007,3009,3010",
"--capabilities=" + capabilities + "," + capabilities,
"--nice-name=system_server",
"--runtime-args",
"--target-sdk-version=" + VMRuntime.SDK_VERSION_CUR_DEVELOPMENT,
"com.android.server.SystemServer",
};
ZygoteConnection.Arguments parsedArgs = null;

int pid;

try {
parsedArgs = new ZygoteConnection.Arguments(args);
ZygoteConnection.applyDebuggerSystemProperty(parsedArgs);
ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);

boolean profileSystemServer = SystemProperties.getBoolean(
"dalvik.vm.profilesystemserver", false);
if (profileSystemServer) {
parsedArgs.runtimeFlags |= Zygote.PROFILE_SYSTEM_SERVER;
}

/* Request to fork the system server process */
pid = Zygote.forkSystemServer(
parsedArgs.uid, parsedArgs.gid,
parsedArgs.gids,
parsedArgs.runtimeFlags,
null,
parsedArgs.permittedCapabilities,
parsedArgs.effectiveCapabilities);
} catch (IllegalArgumentException ex) {
throw new RuntimeException(ex);
}

2.SystemServer.main方法

1
2
3
public static void main(String[] args) {
new SystemServer().run();
}

run方法

2.1. 创建loop对象
1
2
// Prepare the main looper thread (this thread).
Looper.prepareMainLooper();
2.2 创建了系统上下文
1
2
// Initialize the system context.
createSystemContext();

创建AcitivityThread
这里创建的ActivityThread 和ActivityThread.main()中创建的不是同一个

1
2
3
4
5
6
7
8
9
10
private void createSystemContext() {
// systemMain()中创建activityThread 调用activityThread .attach(true, 0); 会创建Application,Instrumentation ,并调用applicition.onCreate()
ActivityThread activityThread = ActivityThread.systemMain();
//创建context,创建LoadedApk
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);

final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
}

2.3启动SystemServiceManager
1
2
3
4
5
6
7
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setStartInfo(mRuntimeRestart,
mRuntimeStartElapsedTime, mRuntimeStartUptime);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// Prepare the thread pool for init tasks that can be parallelized
SystemServerInitThreadPool.get();
2.4 启动相关服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Start services.
try {
traceBeginAndSlog("StartServices");
startBootstrapServices();
startCoreServices();
startOtherServices();
SystemServerInitThreadPool.shutdown();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
traceEnd();
}

2.4.1startBootstrapServices()启动了ActivityManagerService ,PowerManagerService,PackageManagerService …等

2.4.2
startCoreServices()启动了BatteryService,UsageStatsService,WebViewUpdateService

2.4.3 startOtherServices()启动了AlarmManagerService,BluetoothService,NotificationManagerService …等

mActivityManagerService.systemReady 启动launcher

1
2
3
4
5
6
// We now tell the activity manager it is okay to run third party
// code. It will call back into us once it has gotten to the state
// where third party code can really run (but before it has actually
// started launching the initial applications), for us to complete our
// initialization.
mActivityManagerService.systemReady(...)

systemReady 方法中调用 mActivityManagerService.startHomeActivityLocked启动launcher

1
2

startHomeActivityLocked(currentUserId, "systemReady");

下面代码1处调用ActivityStarter中的 startActivityMayWait 或者startActivity ,又开始走Activity启动流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
mSupervisor.moveHomeStackTaskToTop(reason);
//代码1
mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason)
.setOutActivity(tmpOutRecord)
.setCallingUid(0)
.setActivityInfo(aInfo)
.execute();
mLastHomeActivityStartRecord = tmpOutRecord[0];
if (mSupervisor.inResumeTopActivity) {
// If we are in resume section already, home activity will be initialized, but not
// resumed (to avoid recursive resume) and will stay that way until something pokes it
// again. We need to schedule another resume.
mSupervisor.scheduleResumeTopActivities();
}
}

2.5进入消息循环
1
2
// Loop forever.
Looper.loop();