基于Andoird 4.2.2的Account Manager源代码分析学习:AccountManagerService系统服务的添加
从启动说起
Android系统加载时,首先启动init进程,该进程会启动Zygote进程。Zygote进程执行/system/bin/app_process程序。app_process程序在执行中,通过AppRuntime::start()函数来创建虚拟机实例,并注册JNI方法。
[cpp]
int main(int argc, const char* const argv[])
{
...
if (zygote) {
runtime.start("com.android.internal.os.ZygoteInit",
startSystemServer ? "start-system-server" : "");
}
...
}
int main(int argc, const char* const argv[])
{
...
if (zygote) {
runtime.start("com.android.internal.os.ZygoteInit",
startSystemServer ? "start-system-server" : "");
}
...
}
看一下AppRuntime::start()函数的说明:
[cpp]
/*
* Start the Android runtime. This involves starting the virtual machine
* and calling the "static void main(String[] args)" method in the class
* named by "className".
*
* Passes the main function two arguments, the class name and the specified
* options string.
*/
void AndroidRuntime::start(const char* className, const char* options)
{
...
}
/*
* Start the Android runtime. This involves starting the virtual machine
* and calling the "static void main(String[] args)" method in the class
* named by "className".
*
* Passes the main function two arguments, the class name and the specified
* options string.
*/
void AndroidRuntime::start(const char* className, const char* options)
{
...
}
这就是说,这个函数做两件事情:首先虚拟机,其次执行className参数提供的Java类的main()方法。
这里传入className是"com.android.internal.os.ZygoteInit",那么看一下它的main()方法:
[java]
public static void main(String argv[]) {
...
try {
if (argv[1].equals("start-system-server")) {
startSystemServer();
...
}
public static void main(String argv[]) {
...
try {
if (argv[1].equals("start-system-server")) {
startSystemServer();
...
}
看一下startSystemServer()方法的实现:
[java]
private static boolean startSystemServer()
throws MethodAndArgsCaller, RuntimeException {
…
int pid;
try {
…
/* Request to fork the system server process */
pid = Zygote.forkSystemServer(
parsedArgs.uid, parsedArgs.gid,
parsedArgs.gids,
parsedArgs.debugFlags,
null,
parsedArgs.permittedCapabilities,
parsedArgs.effectiveCapabilities);
} catch (IllegalArgumentException ex) {
throw new RuntimeException(ex);
}
/* For child process */
if (pid == 0) {
handleSystemServerProcess(parsedArgs);
}
return true;
}
private static boolean startSystemServer()
throws MethodAndArgsCaller, RuntimeException {
…
int pid;
try {
…
/* Request to fork the system server process */
pid = Zygote.forkSystemServer(
parsedArgs.uid, parsedArgs.gid,
parsedArgs.gids,
parsedArgs.debugFlags,
null,
parsedArgs.permittedCapabilities,
parsedArgs.effectiveCapabilities);
} catch (IllegalArgumentException ex) {
throw new RuntimeException(ex);
}
/* For child process */
if (pid == 0) {
handleSystemServerProcess(parsedArgs);
}
return true;
}
在这里,Zygote进程fork出SystemServer进程,随后再handleSystemServerProcess()方法中对它做初始处理,包括关闭克隆Zygote进程所带来的socket。
接下来,调用RuntimeInit.zygoteInit()方法完成其它的初始化工作。其中,将会调用SystemServer.main()方法进入SystemServer本身的初始化。main()方法经过一系列的步骤之后,调用本地方法init1()来启动Su易做图ceFlinger和SensorService这样的关键服务。之后init1()从本地回调Java层的SystemServer.init2()方法来启动Java层的各项服务:
[java]
public static final void init2() {
Slog.
补充:移动开发 , Android ,