ServiceManager管理的服务——《android内核剖析》笔记

Android系统中有很多系统Service,例如AccountManagerService,LocationService, InputMethodManagerService等等。所有的系统Service统一由ServiceManagerService进行管理,其它系统Service启动时,首先通过ServiceManager.addService()方法把自己的Binder对象注册到ServiceManagerService。当应用程序要使用某个系统服务时,首先通过ServiceManager获取相应服务的Binder对象,然后再通过该Binder对象访问相应的系统服务。

ServiceManager服务的Binder对象通过BinderInternal.getContextObject()方法获取,ServiceManager.getIServiceManager()方法中获取ServiceManager接口的代码如下:

// Find theservice manager
sServiceManager =ServiceManagerNative.asInterface(BinderInternal.getContextObject());

 

下面以InputMethodManagerService的获取为例说明系统Service的调用过程,下图为应用程序调用InputMethodManagerService的流程:

         

应用程序调用ContextImpl.getSystemService(INPUT_METHOD_SERVICE)获取InputMethodManager,getSystemService()中会调用InputMethodManager的getInstance()方法。getInstance()方法的代码如下:

static publicInputMethodManager getInstance(Looper mainLooper) {
        synchronized (mInstanceSync){
            if (mInstance != null) {
                return mInstance;
            }
            IBinder b =
                  ServiceManager.getService(Context.INPUT_METHOD_SERVICE);
            IInputMethodManager service =               
                  IInputMethodManager.Stub.asInterface(b);
            mInstance = newInputMethodManager(service, mainLooper);
        }
 
        return mInstance;
}

ServiceManager.getService()通过前面的sServiceManager向ServiceManagerService发起IPC调用,获取InputMethodManagerService的Binder对象,获取该Binder对象后即可创建InputMethodManager对象。最后,应用程序通过返回的InputMethodManager对象调用InputMethodManagerService提供的服务 。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值