Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android開發之SystemService

Android開發之SystemService

編輯:關於android開發


       SystemServer是Android系統的一個核心進程,它是由zygote進程創建的,因此在android的啟動過程中位於zygote之後。android的所有服務循環都是建立在 SystemServer之上的。在SystemServer中,將可以看到它建立了android中的大部分服務,並通過ServerManager的add_service方法把這些服務加入到了ServiceManager的svclist中。從而完成ServcieManager對服務的管理。

       先看下SystemServer的main函數:

java代碼:
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"); //加載本地庫android_servers
init1(args);
}


        在main函數中主要是調用了本地方法init1(args), 他的實現位於../base/services/jni/com_android_server_SystemService.cpp中

java代碼:
static voidandroid_server_SystemServer_init1(JNIEnv* env, jobject clazz)
{
system_init();
}


       進一步來看system_init,在這裡面看到了閉合循環管理框架:

java代碼:
runtime->callStatic("com/android/server/SystemServer","init2");
//回調了SystemServer.java中的init2方法
if (proc->supportsProcesses()) {
LOGI("System server: enteringthread pool.n");
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
LOGI("System server: exitingthread pool.n");
}


        通過調用com/android/server/SystemServer.java中的init2方法完成service的注冊。在init2方法中主要建立了以ServerThread線程,然後啟動線程來完成service的注冊。

java代碼:
public static final void init2() {
Slog.i(TAG, "Entered the Androidsystem server!");
Thread thr = new ServerThread();
thr.setName("android.server.ServerThread");
thr.start();
}


       具體實現service的注冊在ServerThread的run方法中:

java代碼:
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));
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved