美文网首页
Android Framework系列3 系统服务

Android Framework系列3 系统服务

作者: 无为3 | 来源:发表于2021-02-24 20:37 被阅读0次

    你知道怎么添加一个系统服务吗?
    要想弄清楚这个问题,先弄明白以下三个问题:

    • 如何使用系统服务
    • 系统服务调用的基本原理
    • 系统服务的注册原理


      看下这个图:
      Service.png

    1). 有一个Service,可能有一堆Client要用它;首先Client得先拿到Service的binder,拿到binder后再向service发起binder调用。
    2). 问题是Client不知道怎么拿到Service的binder;典型的做法是Service把它的binder注册到一个固定的地方也就是ServiceManager,当Client要用service的时候就通过这个service的名称,去ServiceManager中查到service的binder,再用这个binder向service发起调用。


    由此总结下,我们要添加一个系统服务的话,需要考虑这么几个点:

    • 使用方便:也就是别的client可以很方便的拿到你的服务。
    • 开放:服务如何开放让别人知道。
    • 跨进程:不论你是跟应用进程通信的,还是跟这个ServiceManager通信都需要跨进程的。

    使用方便

    我们回想一下我们平时是怎么样使用系统服务的,就是通过这个
    Context.getSystemService函数,这里的传入了这个服务的名称,然后这个函数会返回这个系统服务的接口对象:

    // 代码来自Android23中:ContextImpl.java
    public Object getSystemService(String name) {
            return SystemServiceRegistry.getSystemService(this, name);
        }
    
    // 代码来自Android23中:SystemServiceRegistry.java
    private static final HashMap<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
                new HashMap<String, ServiceFetcher<?>>();
    public static Object getSystemService(ContextImpl ctx, String name) {
            ServiceFetcher<?> fetcher = SYSTEM_SERVICE_FETCHERS.get(name);
            return fetcher != null ? fetcher.getService(ctx) : null;
        }
    
    • 通过服务的名称拿到服务对应的ServiceFetcher(通过名称在HashMap中拿到的)
    • 再用ServiceFetcher的getService函数来获取这个系统服务的接口对象。



    接下来看下ServiceFetcher.getService

    // 代码来自Android23中:SystemServiceRegistry.java
    public final T getService(ContextImpl ctx) {
                final Object[] cache = ctx.mServiceCache;
                synchronized (cache) {
                    // Fetch or create the service.
                    Object service = cache[mCacheIndex];
                    if (service == null) {
                        service = createService(ctx);
                        cache[mCacheIndex] = service;
                    }
                    return (T)service;
                }
            }
    
    • 获取context中的缓存mServiceCache(数组,跟Context挂钩,有几个Context就有几个缓存)
    • 如果命中缓存则直接返回服务的接口对象
    • 没命中则通过createService获取,然后再存入缓存中



    再接着往下createService
    以一个具体的createService为例

    // 代码来自Android23中:SystemServiceRegistry.java
    registerService(Context.POWER_SERVICE, PowerManager.class,
                    new CachedServiceFetcher<PowerManager>() {
                @Override
                public PowerManager createService(ContextImpl ctx) {
                    IBinder b = ServiceManager.getService(Context.POWER_SERVICE);
                    IPowerManager service = IPowerManager.Stub.asInterface(b);
                    return new PowerManager(ctx.getOuterContext(),
                            service, ctx.mMainThread.getHandler());
                }});
    
    • 从ServiceManager里边,根据这个Service的名称拿到了他的IBinder对象,IBinder其实就是一个BinderProxy(具体细节Binder章节再讲)。
    • 根据这个BinderProxy封装了一个IPowerManager类。
    • 再封装了一层,new了一个PowerManager对象返回。(为什么还要对BinderProxy封装,因为PowerManager就是一个静态代理类,里面每个函数实际都是调PowerManager方法加上try catch)



    继续往下看ServiceManager.getService的实现

    // 代码来自Android23中:ServiceManager.java
    public static IBinder getService(String name) {
            try {
                IBinder service = sCache.get(name);
                if (service != null) {
                    return service;
                } else {
                    return getIServiceManager().getService(name);
                }
            } catch (RemoteException e) {
                Log.e(TAG, "error in getService", e);
            }
            return null;
        }
    

    一个典型的缓存机制并不复杂,着重说下getIServiceManager。这个方法其实最终获取的就是ServiceManager它的BinderProxy对象,也就是ServiceManager的业务类。

    服务开放

    一个服务如何开放让别人知道?
    系统服务的注册主要是通过ServiceManager的addService函数:

    public static void addService(String name, IBinder service) {
            try {
                getIServiceManager().addService(name, service, false);
            } catch (RemoteException e) {
                Log.e(TAG, "error in addService", e);
            }
        }
    

    这个函数看着挺简单,有两个入参,一个服务名称,一个服务的Binder对象。首先获取ServiceManager的Binder对象,然后调用这个addService(跨进程调用)。把这个name和service传给ServiceManager。



    系统服务何时注册

    • SystemServer启动 的时候,会启用各类系统服务,其中很重要的一个步骤就是把这些服务他的binder对象注册到ServiceManager里面。
    • 系统服务它并不是完全在SystemServer里面,有一小部分是单独开了一个进程,例如我们熟悉的ServiceManager,SufaceFliger等等这些东西,他们一样注册到ServiceManager,这样别人才能找到它。
    SystemServer启动.png

    跨进程调用

    跨进程需要先启用binder机制,关于这个binder机制详细的在之后的系列文章中再介绍,主要有这么三个步骤:

    • 打开binder驱动
    • 映射内存,分配缓冲区
    • 启动binder线程,进入binder loop

    总结

    相关文章

      网友评论

          本文标题:Android Framework系列3 系统服务

          本文链接:https://www.haomeiwen.com/subject/xrcdfltx.html