转载:http://www.eoeandroid.com/thread-82885-1-1.html
启动应用进程:
我们在上一篇介绍ActivityThread和ActivityManagerService时已经讲过,程序的主入口是在ActivityThread的main函数,activity的startActivity最终是在ActivityManagerService中执行的,那么应用程序的进程是怎么创建的?看下类图:
我们再来看看ActivityManagerService中的startProcessLocked方法。
java代码:
-
int pid = Process.start("android.app.ActivityThread",
-
mSimpleProcessManagement ? app.processName : null, uid, uid, gids, debugFlags, null);
-
复制代码
通过Process的start方法来创建进程。
java代码:
-
/ **
-
*通过Zygote进程来创建新的vm进程
-
*/
-
public static final int start(final String processClass,final String niceName,int uid, int gid, int[] gids,int debugFlags,String[] zygoteArgs)
-
{
-
if (supportsProcesses()) {
-
try {
-
return startViaZygote(processClass, niceName, uid, gid, gids,
-
debugFlags, zygoteArgs); //argsForZygote.add("--runtime-init")初始化运行环境
-
} catch (ZygoteStartFailedEx ex) {
-
Log.e(LOG_TAG, "Starting VM process through Zygote failed");
-
throw new RuntimeException(
-
"Starting VM process through Zygote failed", ex);
-
}
-
} else {
-
// Running in single-process mode
-
Runnable runnable = new Runnable() {
-
public void run() {
-
Process.invokeStaticMain(processClass);
-
}
-
};
-
// Thread constructors must not be called with null names (see spec).
-
if (niceName != null) {
-
new Thread(runnable, niceName).start();
-
} else {
-
new Thread(runnable).start();
-
}
-
return 0;
-
}
-
}
-
复制代码