Android 之 SystemService

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. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值