ServiceManager,SystemServer,SystemServiceManager,SystemService—简述这个几个类的关系

SystemServer

这是一个重要的进程,是zygote fork的第一个进程。其中WindowManagerService,ActivityManagerService等重要的可以binder通信的服务都运行在这个SystemServer进程。而像WindowManagerService,ActivityManagerService这样重要,繁忙的服务,是运行在单独线程中,而有些没有繁重的服务,并没有单独开一个线程,有些服务会注册Receiver。

SystemServiceManager

/**
 * Manages creating, starting, and other lifecycle events of
 * {@link com.android.server.SystemService system services}.
 *
 * {@hide}
 */

这是一个创建实例,启动实例,管理实例的生命周期的一个类。这些实例必须实现com.android.server.SystemService。而这个抽象类有一个onStart,SystemServiceManager启动实例的途径就是调用这个方法,在SystemServer中调用SystemServiceManager#startService(Class),代码如下:

/**
     * Creates and starts a system service. The class must be a subclass of
     * {@link com.android.server.SystemService}.
     *
     * @param serviceClass A Java class that implements the SystemService interface.
     * @return The service instance, never null.
     * @throws RuntimeException if the service fails to start.
     */
    @SuppressWarnings("unchecked")
    public <T extends SystemService> T startService(Class<T> serviceClass) {
        final String name = serviceClass.getName();
        Slog.i(TAG, "Starting " + name);

        // Create the service.
        if (!SystemService.class.isAssignableFrom(serviceClass)) {
            throw new RuntimeException("Failed to create " + name
                    + ": service must extend " + SystemService.class.getName());
        }
        final T service;
        try {
            Constructor<T> constructor = serviceClass.getConstructor(Context.class);
            service = constructor.newInstance(mContext);
        } catch (InstantiationException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service could not be instantiated", ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service must have a public constructor with a Context argument", ex);
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service must have a public constructor with a Context argument", ex);
        } catch (InvocationTargetException ex) {
            throw new RuntimeException("Failed to create service " + name
                    + ": service constructor threw an exception", ex);
        }

        // Register it.
        mServices.add(service);

        // Start it.
        try {
            service.onStart();
        } catch (RuntimeException ex) {
            throw new RuntimeException("Failed to start service " + name
                    + ": onStart threw an exception", ex);
        }
        return service;
    }

改方法是通过反射构造方法的方式去创建实例的,所以子类需要有一个参数为Context的构造方法。然后将给实例存入一个ArrayList,最后调用实例的onstart()。这些由SystemServiceManager创建并启动的服务,都是继承了SystemService,但是并没有实现IBinder,所以是不可以进程Binder通信的。但是绝大部分SystemService都会在onStart中调用SystemService#publishBinderService(String name, IBinder service, boolean allowIsolated)方法。代码如下:

/**
     * Publish the service so it is accessible to other services and apps.
     */
    protected final void publishBinderService(String name, IBinder service,
            boolean allowIsolated) {
        ServiceManager.addService(name, service, allowIsolated);
    }

就是在onStart中会注册Binder服务。

ServiceManager

这个类的主要方法有addService(),getService(),listServices()。所以这个类的主要职责是控制用户访问服务,控制服务是否可以使用这一接口(通过注册时的检查权限),管理Service。ServiceManager并不负责创建和启动服务实例,一般服务的启动都是在创建服务实例时完成的,而没有额外调用一个方法去启动它。

理清这些关系后,分析系统服务的重心就是,对于每个服务逐个击破,比如从ActivityManagerService的main方法开试分析其初始化和启动过程。

在SystemServer中启动的各种服务,在ServiceManager中注册的是用于进程间通信的,而用于system_server进程内部通信的,则注册到LocalServces中,而LocalServices中都是静态方法,由SystemServiceManager启动并管理的是继承了SystemService的。SystemService中很多方法都是通过操作LocalServices。ActivityManagerService也会有相关内部类的实例注册到LocalServices,这个实例是提供system server进程内部的其他服务操作AMS的接口。其他服务也是这样,把该服务在system server内部使用到的接口封装到一个独立的类中,把这个类的实例注册到LocalServices,而给其他进程调用的接口封装到一个Binder类中,然后注册到ServiceManager中。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值