相关源码:
packages/services/Telephony
- src/com/android/phone/PhoneApp.java
- AndroidManifest.xml
frameworks/base/services/core/java/com/android/server/
- am/ActivityManagerService.java
frameworks/base/services/java/com/android/server/
- SystemServer.java
frameworks/base/core/java/android/os/
- Process.java
复制代码
在packages/services/Telephony/AndroidManifest.xml中,PhoneApp的属性android:persistent="true",ActivityManagerService在启动后会去遍历所有android:persistent="true"的应用,而后交由Process通过socket通知zygote fork一个新的进程,phone进程启动具体过程如下:
1 注册ActivityManagerService
SystemServer执行main方法,接着执行run,在run中有这样一段
frameworks/base/services/java/com/android/server/SystemServer.java
private void run() {
startBootstrapServices();
startCoreServices();
startOtherServices();
}
复制代码
其中的startBootstrapServices方法去调用startService启动ActivityManagerService。
frameworks/base/services/java/com/android/server/SystemServer.java
private void startBootstrapServices() {
// Activity manager runs the show.
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
...
// Set up the Application instance for the system process and get started.
mActivityManagerService.setSystemProcess();
}
复制代码
startService会回调Lifecycle.onStart(),这时已经创建好ActivityManagerService对象mActivityManagerService,然后在SystemServer的startBootstrapServices中继续调用ActivityManagerService的setSystemProcess,
在ServiceManager中完成注册ActivityManagerService。 frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public void setSystemProcess() {
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
}
复制代码
至此ActivityManagerService完成了初始化。
2 遍历persistent进程
SystemServer执行startBootstrapServices完成AMS的初始化后,继续调用startOtherServices。 frameworks/base/services/java/com/android/server/SystemServer.java
private void startOtherServices() {
mActivityManagerService.systemReady(new Runnable() ...
}
复制代码
在ActivityManagerService.systemReady中遍历所有persistent进程。 frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
public void systemReady(final Runnable goingCallback) {
...
synchronized (this) {
if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
try {//遍历所有android:persistent属性为true的进程,PhoneApp的persistent也为true
List apps = AppGlobals.getPackageManager().
getPersistentApplications(STOCK_PM_FLAGS);
if (apps != null) {
int N = apps.size();
int i;
for (i=0; iif (info != null &&
!info.packageName.equals("android")) {
addAppLocked(info, false, null /* ABI override */);//persistent进程启动,Phone进程也不例外
}
}
}
} catch (RemoteException ex) {
// pm is in same process, this will never happen.
}
}
// Start up initial activity.
mBooting = true;
startHomeActivityLocked(mCurrentUserId, "systemReady");//启动launcher
...
}
复制代码
接着进入addAppLocked,启动persistent进程 frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated,
String abiOverride) {
startProcessLocked(app, "added application", app.processName, abiOverride,
null /* entryPoint */, null /* entryPointArgs */);
}
复制代码
需要继续调用startProcessLocked,尤其注意if (entryPoint == null) entryPoint = "android.app.ActivityThread";
,entryPoint将作为zygote fork phone进程的参数。 frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
private final void startProcessLocked(ProcessRecord app, String hostingType,
String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) {
...
// Start the process. It will either succeed and return a result containing
// the PID of the new process, or else throw a RuntimeException.
boolean isActivityProcess = (entryPoint == null);
if (entryPoint == null) entryPoint = "android.app.ActivityThread";//记住这个类名,zygote会反射调用其main()方法
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "Start proc: " +
app.processName);
checkTime(startTime, "startProcess: asking zygote to start proc");
Process.ProcessStartResult startResult = Process.start(entryPoint,
app.processName, uid, uid, gids, debugFlags, mountExternal,
app.info.targetSdkVersion, app.info.seinfo, requiredAbi, instructionSet,
app.info.dataDir, entryPointArgs);
...
}
复制代码
3 fork phone进程
Process.start继续调用startViaZygote,startViaZygote继续调zygoteSendArgsAndGetResult,通过LocalSocket,带上类名等参数,if (entryPoint == null) entryPoint = "android.app.ActivityThread"
,通知Zygote fork phone进程,Zygote通过native方法 fork phone进程后,会反射进入ActivityThread的main方法,main继续调用Application的onCreate。 frameworks/base/core/java/android/os/Process.java
private static ProcessStartResult zygoteSendArgsAndGetResult(ZygoteState zygoteState, ArrayList args){
int sz = args.size();
for (int i = 0; i < sz; i++) {
String arg = args.get(i);
if (arg.indexOf('\n') >= 0) {
throw new ZygoteStartFailedEx(
"embedded newlines not allowed");
}
writer.write(arg);
writer.newLine();
}
writer.flush();
}
复制代码
4 PhoneApp 入口
PhoneApp继承了Application,进程fork后,ActivityThread会去调用其onCreate方法,之后就可以进入电话、数据连接、短彩信流程了。
总结
在ActivityManagerService启动之后,AMS会遍历所有persistent为true的应用,通过过LocalSocket通知Zygote fork phone进程,接着反射执行ActivityThread的main方法,ActivityThread会去调用PhoneApp的onCreate方法。
Phone Application对象的创建