SystemService publishBinderService 和 publishLocalService

publishBinderService

通过 Binder 将服务注册到 ServiceManager,为了跨进程访问

/**
 * Publish the service so it is accessible to other services and apps.
 *
 * @param name the name of the new service
 * @param service the service object
 */
protected final void publishBinderService(String name, IBinder service) {
	publishBinderService(name, service, false);
}

   /**
    * Publish the service so it is accessible to other services and apps.
    *
    * @param name the name of the new service
    * @param service the service object
    * @param allowIsolated set to true to allow isolated sandboxed processes
    * to access this service
    */
   protected final void publishBinderService(String name, IBinder service,
           boolean allowIsolated) {
       publishBinderService(name, service, allowIsolated, DUMP_FLAG_PRIORITY_DEFAULT);
   }

/**
 * Publish the service so it is accessible to other services and apps.
 *
 * @param name the name of the new service
 * @param service the service object
 * @param allowIsolated set to true to allow isolated sandboxed processes
 * to access this service
 * @param dumpPriority supported dump priority levels as a bitmask
 */
protected final void publishBinderService(String name, IBinder service,
		boolean allowIsolated, int dumpPriority) {
	ServiceManager.addService(name, service, allowIsolated, dumpPriority);
}

publishLocalService

通过这种方式注册的服务,不是binder对象,只能再同一进程使用,也就是system_server 进程

    /**
     * Publish the service so it is only accessible to the system process.
     */
    protected final <T> void publishLocalService(Class<T> type, T service) {
        LocalServices.addService(type, service);
    }
/**
 * This class is used in a similar way as ServiceManager, except the services registered here
 * are not Binder objects and are only available in the same process.
 *
 * Once all services are converted to the SystemService interface, this class can be absorbed
 * into SystemServiceManager.
 *
 * {@hide}
 */
public final class LocalServices {
    private LocalServices() {}

    private static final ArrayMap<Class<?>, Object> sLocalServiceObjects =
            new ArrayMap<Class<?>, Object>();

    /**
     * Returns a local service instance that implements the specified interface.
     *
     * @param type The type of service.
     * @return The service object.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getService(Class<T> type) {
        synchronized (sLocalServiceObjects) {
            return (T) sLocalServiceObjects.get(type);
        }
    }

    /**
     * Adds a service instance of the specified interface to the global registry of local services.
     */
    public static <T> void addService(Class<T> type, T service) {
        synchronized (sLocalServiceObjects) {
            if (sLocalServiceObjects.containsKey(type)) {
                throw new IllegalStateException("Overriding service registration");
            }
            sLocalServiceObjects.put(type, service);
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值