SystemServer是Android系統(tǒng)的一個(gè)核心進(jìn)程,它是由zygote進(jìn)程創(chuàng)建的,因此在android的啟動(dòng)過(guò)程中位于zygote之后。android的所有服務(wù)循環(huán)都是建立在 SystemServer之上的。在SystemServer中,將可以看到它建立了android中的大部分服務(wù),并通過(guò)ServerManager的add_service方法把這些服務(wù)加入到了ServiceManager的svclist中。從而完成ServcieManager對(duì)服務(wù)的管理。
先看下SystemServer的main函數(shù):
- native public static void init1(String[]args);
- public static void main(String[] args) {
- if(SamplingProfilerIntegration.isEnabled()) {
- SamplingProfilerIntegration.start();
- timer = new Timer();
- timer.schedule(new TimerTask() {
- @Override
- public void run() {
- SamplingProfilerIntegration.writeSnapshot("system_server");
- }
- }, SNAPSHOT_INTERVAL,SNAPSHOT_INTERVAL);
- }
- // The system server has to run all ofthe time, so it needs to be
- // as efficient as possible with itsmemory usage.
- VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
- System.loadLibrary("android_servers"); //加載本地庫(kù)android_servers
- init1(args);
- }
在main函數(shù)中主要是調(diào)用了本地方法init1(args), 他的實(shí)現(xiàn)位于../base/services/jni/com_android_server_SystemService.cpp中
- static voidandroid_server_SystemServer_init1(JNIEnv* env, jobject clazz)
- {
- system_init();
- }
進(jìn)一步來(lái)看system_init,在這里面看到了閉合循環(huán)管理框架:
- runtime->callStatic("com/android/server/SystemServer","init2");//回調(diào)了SystemServer.java中的init2方法
- if (proc->supportsProcesses()) {
- LOGI("System server: enteringthread pool.\n");
- ProcessState::self()->startThreadPool();
- IPCThreadState::self()->joinThreadPool();
- LOGI("System server: exitingthread pool.\n");
- }
通過(guò)調(diào)用com/android/server/SystemServer.java中的init2方法完成service的注冊(cè)。在init2方法中主要建立了以ServerThread線程,然后啟動(dòng)線程來(lái)完成service的注冊(cè)。
- public static final void init2() {
- Slog.i(TAG, "Entered the Androidsystem server!");
- Thread thr = new ServerThread();
- thr.setName("android.server.ServerThread");
- thr.start();
- }
具體實(shí)現(xiàn)service的注冊(cè)在ServerThread的run方法中:
- try {
- Slog.i(TAG, "EntropyService");
- ServiceManager.addService("entropy", new EntropyService());
- Slog.i(TAG, "PowerManager");
- power = new PowerManagerService();
- ServiceManager.addService(Context.POWER_SERVICE, power);
- Slog.i(TAG, "ActivityManager");
- context =ActivityManagerService.main(factoryTest);
- Slog.i(TAG, "TelephonyRegistry");
- ServiceManager.addService("telephony.registry", newTelephonyRegistry(context));
-
- }
|