移至 Android 11 后无法绑定到服务 [英] Unable to bind to service after moving to Android 11

查看:137
本文介绍了移至 Android 11 后无法绑定到服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序.一种不使用 Android 11 中引入的任何权限(相机、位置等)的前台服务.我有连接到我的服务的客户端应用程序.在 android 8 中,此连接有效,但是在 Android 11 环境中调用 context.BindService() 时始终返回 false.

I have two applications. One that is a foreground service that does not use any of the permissions introduced in Android 11 (camera, location etc). I have client apps that connect to my service. In android 8 this connection works, however when in the Android 11 environment calls to context.BindService() always return false.

这是服务清单:

<service
            android:name="com.mypackage.myService"
            android:enabled="true"
            android:exported="true"
            tools:ignore="ExportedService">
        </service>

这是我在客户端使用的连接函数:

Here is my connection function used in the client:

fun connect(context: Context): Boolean {
    Intent().also {
        it.component = ComponentName(
            context.getString(R.string.middleware_package),
            context.getString(R.string.middleware_service)
        )
        val bound = context.bindService(it, connection, Context.BIND_AUTO_CREATE)

        if (!bound) {
            Log.e(LOG_TAG, "Unable to connect to service. Is the service installed?")
        }
        return bound
    }
}

在 bindService() 的 android 文档中,它声明它将返回 false 如果系统找不到该服务,或者您的客户端没有绑定到它的权限.";由于我的服务不使用任何新的 android 11 权限,我假设它现在没有正确找到服务?这怎么可能?

In the android docs for bindService() it states it will return false "if the system couldn't find the service or if your client doesn't have permission to bind to it." Since my service does not use any of the new android 11 permissions I'm assuming it now is not finding the service properly? How can this be?

推荐答案

您需要将 <queries> 元素添加到客户端应用程序的清单中,以标识服务应用程序.

You will need to add a <queries> element to the manifest of your client app, identifying the service app.

在带有应用程序的这个示例应用程序com.commonsware.android.r.embed.server的ID,我绑定了服务.

In this sample app with an application ID of com.commonsware.android.r.embed.server, I have a bound service.

这个示例客户端应用中,我将这些行添加到清单中以允许客户端应用程序绑定到服务应用程序:

In this sample client app, I added these lines to the manifest to allow the client app to bind to the service app:

  <queries>
    <package android:name="com.commonsware.android.r.embed.server" />
  </queries>

这篇关于移至 Android 11 后无法绑定到服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆