publishBinderService和publishLocalService

publishBinderService发布的服务会被添加到ServiceManager,允许其他服务和应用通过getService访问。而publishLocalService将服务添加到LocalServices的映射中,仅供系统进程访问。publishBinderService有多个重载方法,控制是否允许隔离沙盒进程访问服务。
摘要由CSDN通过智能技术生成

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);
    }
### 回答1: publishbinderservice是一个Android系统中的服务,它的作用是将一个Binder对象发布到系统服务中,以便其他应用程序可以通过该Binder对象与该服务进行通信。通过publishbinderservice,应用程序可以将自己的服务注册到系统服务中,从而提供给其他应用程序使用。这个服务在Android系统中非常重要,因为它为应用程序之间的通信提供了一个标准的接口。 ### 回答2: publishbinderservice 是一种在Android平台上实现跨进程通信的方法。在Android系统中,每个应用程序都处于独立的进程中,为了实现应用程序的互相调用和数据传递,Android提供了Binder机制。publishbinderservice 就是通过在一个应用程序中发布一个Binder服务,使其他应用程序可以通过Binder来与该服务进行交互。 具体实现 publishbinderservice 的方法如下: 1. 首先,在 AndroidManifest.xml 文件中声明一个 Service,并在其内部定义一个继承自 Binder 的子类。这个子类将作为服务的核心,用于处理其他应用程序发送的请求。 2. 在 Service 的 onBind 方法中,返回这个子类的实例,使得其他应用程序可以通过同样的方式绑定到这个服务。 3. 创建一个 ServiceConnection 对象,用于监听其他应用程序与服务的连接状态,并在连接成功时返回一个 IBinder 对象。 4. 在需要与服务进行交互的应用程序中,调用 bindService 方法绑定到这个服务,并在 onBind 方法中获取到返回的 IBinder 对象。 5. 获取到返回的 IBinder 对象后,通过一个自定义的接口来实现应用程序与服务之间的通信,可以调用服务中定义的方法或传递数据。 通过使用 publishbinderservice,我们可以实现不同应用程序之间的数据共享和交互,提升了应用程序的灵活性和功能性。同时,publishbinderservice 还提供了一种安全的跨进程通信方式,可以限制其他应用程序对服务的访问权限,确保数据的安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无敌的娃哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值