SystemServer
SystemServer类
注:SystemServer是zygote进程fork的第一个进程。
SystemServer启动的service是指android中和binder通讯有关的service端。与apk中的service是不同的概念。
@frameworks\base\services\java\com\android\server\ SystemServer.java
public class SystemServer { private static final String TAG = "SystemServer"; … native public static void init1(String[] args);
public static void main(String[] args) { … System.loadLibrary("android_servers"); … init1(args); }
public static final void init2() { Slog.i(TAG, "Entered the Android system server!"); Thread thr = new ServerThread(); thr.setName("android.server.ServerThread"); thr.start(); } |
注:main启动init1方法
Init1方法为native方法,在libandroid_servers.so中
init1 将回调 init2 方法
init1
对应的jni为:
@frameworks\base\services\jni\ com_android_server_SystemServer.cpp
static void android_server_SystemServer_init1(JNIEnv* env, jobject clazz) { system_init(); } |
system_init 在libsystem_server.so中
@ frameworks\base\cmds\system_server\library\ system_init.cpp
extern "C" status_t system_init() { LOGI("Entered system_init()");
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager(); LOGI("ServiceManager: %p\n", sm.get());
sp<GrimReaper> grim = new GrimReaper(); sm->asBinder()->linkToDeath(grim, grim.get(), 0);
char propBuf[PROPERTY_VALUE_MAX]; property_get("system_init.startsurfaceflinger", propBuf, "1"); if (strcmp(propBuf, "1") == 0) { // Start the SurfaceFlinger SurfaceFlinger::instantiate(); }
property_get("system_init.startsensorservice", propBuf, "1"); if (strcmp(propBuf, "1") == 0) { // Start the sensor service SensorService::instantiate(); }
// And now start the Android runtime. We have to do this bit // of nastiness because the Android runtime initialization requires // some of the core system services to already be started. // All other servers should just start the Android runtime at // the beginning of their processes's main(), before calling // the init function. LOGI("System server: starting Android runtime.\n"); AndroidRuntime* runtime = AndroidRuntime::getRuntime();
LOGI("System server: starting Android services.\n"); JNIEnv* env = runtime->getJNIEnv(); if (env == NULL) { return UNKNOWN_ERROR; } jclass clazz = env->FindClass("com/android/server/SystemServer"); if (clazz == NULL) { return UNKNOWN_ERROR; } jmethodID methodId = env->GetStaticMethodID(clazz, "init2", "()V"); if (methodId == NULL) { return UNKNOWN_ERROR; } env->CallStaticVoidMethod(clazz, methodId);
LOGI("System server: entering thread pool.\n"); ProcessState::self()->startThreadPool(); IPCThreadState::self()->joinThreadPool(); LOGI("System server: exiting thread pool.\n");
return NO_ERROR; |
注:这里启动了两个service:SurfaceFlinger 和SensorService
启动android runtime
回调com.android.server.SystemServer init2方法启动动其他service。
Init2