publishBinderService和publishLocalService

publishBinderService和publishLocalService

添加系统服务的时候,你会发现官方使用了publishBinderService和publishLocalService,了解一下它们的作用和区别,总结一句话就是publishBinderService发布的服务被加到ServiceManager中了,publishLocalService发布的服务被添加到LocalService的map中了,前者可以通过getService获得,后者仅能有系统进程访问

OS:Android 13

路径: frameworks/base/services/core/java/com/android/server/SystemService.java

publishLocalService: 给服务加入到了LocalServices中,只能由系统进程访问

    /**
     * Publish the service so it is only accessible to the system process.
     *
     * @hide
     */
    protected final <T> void publishLocalService(Class<T> type, T service) {
        LocalServices.addService(type, service);
    }

publishBinderService: 最终调用了4个参数的方法,将服务添加到了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(@NonNull String name, @NonNull 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(@NonNull String name, @NonNull 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
     *
     * @hide
     */
    protected final void publishBinderService(String name, IBinder service,
            boolean allowIsolated, int dumpPriority) {
        ServiceManager.addService(name, service, allowIsolated, dumpPriority);
    }

你可能感兴趣的:(android,service,Framework)