现在的位置: 首页 > 综合 > 正文

Android入门进阶教程(12)-SystemService详解

2017年12月17日 ⁄ 综合 ⁄ 共 2600字 ⁄ 字号 评论关闭

SystemServer是Android系统的一个核心进程,它是由zygote进程创建的,因此在android的启动过程中位于zygote之后。android的所有服务循环都是建立在 SystemServer之上的。在SystemServer中,将可以看到它建立了android中的大部分服务,并通过ServerManager的add_service方法把这些服务加入到了ServiceManager的svclist中。从而完成ServcieManager对服务的管理。

先看下SystemServer的main函数:

[java] view
plain
copy

  1. native public static void init1(String[]args);  
  2. public static void main(String[] args) {  
  3.        if(SamplingProfilerIntegration.isEnabled()) {  
  4.           SamplingProfilerIntegration.start();  
  5.            timer = new Timer();  
  6.            timer.schedule(new TimerTask() {  
  7.                @Override  
  8.                public void run() {  
  9.                   SamplingProfilerIntegration.writeSnapshot("system_server");  
  10.                }  
  11.            }, SNAPSHOT_INTERVAL,SNAPSHOT_INTERVAL);  
  12.        }  
  13.        // The system server has to run all ofthe time, so it needs to be  
  14.        // as efficient as possible with itsmemory usage.  
  15.       VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);  
  16.       System.loadLibrary("android_servers"); //加载本地库android_servers  
  17.        init1(args);  
  18.    }  

在main函数中主要是调用了本地方法init1(args), 他的实现位于../base/services/jni/com_android_server_SystemService.cpp中

[java] view
plain
copy

  1. static voidandroid_server_SystemServer_init1(JNIEnv* env, jobject clazz)  
  2. {  
  3.     system_init();  
  4. }  

   进一步来看system_init,在这里面看到了闭合循环管理框架:

[java] view
plain
copy

  1. runtime->callStatic("com/android/server/SystemServer","init2");//回调了SystemServer.java中的init2方法  
  2.     if (proc->supportsProcesses()) {  
  3.         LOGI("System server: enteringthread pool.\n");  
  4.        ProcessState::self()->startThreadPool();  
  5.        IPCThreadState::self()->joinThreadPool();  
  6.         LOGI("System server: exitingthread pool.\n");  
  7.     }  

通过调用com/android/server/SystemServer.java中的init2方法完成service的注册。在init2方法中主要建立了以ServerThread线程,然后启动线程来完成service的注册。

[java] view
plain
copy

  1. public static final void init2() {  
  2.     Slog.i(TAG, "Entered the Androidsystem server!");  
  3.     Thread thr = new ServerThread();  
  4.    thr.setName("android.server.ServerThread");  
  5.     thr.start();  
  6. }  

具体实现service的注册在ServerThread的run方法中:

[java] view
plain
copy

  1.  try {  
  2.             Slog.i(TAG, "EntropyService");  
  3.            ServiceManager.addService("entropy"new EntropyService());  
  4.             Slog.i(TAG, "PowerManager");  
  5.             power = new PowerManagerService();  
  6.            ServiceManager.addService(Context.POWER_SERVICE, power);  
  7.             Slog.i(TAG, "ActivityManager");  
  8.             context =ActivityManagerService.main(factoryTest);  
  9.             Slog.i(TAG, "TelephonyRegistry");  
  10.            ServiceManager.addService("telephony.registry", newTelephonyRegistry(context));  
  11.   
  12. }  


抱歉!评论已关闭.