在Android 11上,当从客户端应用程序调用时,远程绑定服务的绑定失败

3

Android 11上,从客户端应用程序调用远程绑定服务会失败。

问题仅限于Android 11。

1:使用具有AIDL接口的远程绑定服务。该服务源自服务B,后者也使用AIDL接口。

public class IPCEnterpriseServicePcsc extends IPCServicePcsc {
    ...
    protected IPCEnterpriseInterfaceLicense licenseBinder;
    ...
}

public class IPCEnterpriseInterfaceLicense extends IRemotePcscServiceLicense.Stub {...}

public class IPCServicePcsc extends IPCService {
        ...
    protected IPCInterfacePcsc mBinder;
    ...
}

public class IPCInterfacePcsc extends IRemotePcscService.Stub{...}

2. 下面是定义服务的服务器应用程序清单:

    <service android:label="@string/pcsc_service_name" android:name=".IPCEnterpriseServicePcsc" >
        <intent-filter>
            <action android:name="com.baimobile.android.enterprise.credential.service.ipc.action.BIND_PCSC" />
            <action android:name="com.baimobile.android.enterprise.credential.service.ipc.action.BIND_LICENSE" />
            <action android:name="com.baimobile.android.enterprise.credential.service.license.action.VALIDATE_USER_LICENSE_INFO" />
        </intent-filter>
    </service>

server app id is "com.baimobile.android.enterprise.credential.service"

3.1 以下是从客户端应用程序调用服务 'IPCEnterpriseServicePcsc' 的代码:

Intent intent = new Intent("com.baimobile.android.enterprise.credential.service.ipc.action.BIND_LICENSE"); intent.setPackage("com.baimobile.android.enterprise.credential.service"); intent.putExtra("Interface","IRemotePcscServiceLicense");

boolean pcscServiceInstalled = appContext.bindService(intent, connectionToPcscInterface, Context.BIND_AUTO_CREATE);

3.2: connectionToPcscInterface 被定义为:

private ServiceConnection connectionToPcscInterface = new ServiceConnection() {
    public void onServiceConnected(ComponentName remoteComponent, IBinder binder) {...};
    public void onServiceDisconnected(ComponentName arg0) {..};
}

3.3:在第3.1步成功执行appContext.bindService()后,服务将调用第3.2步中提到的onServiceConnected()。在这里,我们进行一些验证,然后绑定到基类服务IPCServicePcsc。

    Intent intent = new Intent("com.baimobile.android.pcsclite.service.ipc.action.BIND_PCSC");
    intent.setPackage("com.baimobile.android.enterprise.credential.service");
    intent.putExtra("Interface","IRemotePcscService");          // Request the PC/SC interface instead of the license interface.
    pcscServiceInstalled = appContext.bindService(intent, connectionToPcscInterface, Context.BIND_AUTO_CREATE);
    if( ! pcscServiceInstalled ){
        Log.e("TAG","bindService failed.");
    }

问题陈述: 在Android 10之前,客户端应用程序能够很好地连接这些服务,但是在Android 11上,第3.3步骤下的绑定失败。
有任何想法是什么原因导致了这个问题,并寻求解决方案的建议。似乎在Android 11上出现了一些破损或加强的情况。
2个回答

11

如果您还没有这样做,您的客户端应用程序需要在清单中添加<queries>元素以识别服务应用程序。

例如,在这个客户端应用程序中,我有:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.commonsware.android.r.embed.client">
  <queries>
    <package android:name="com.commonsware.android.r.embed.server" />
  </queries>

  <application
    android:name=".MainApp"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>

</manifest>

<queries>元素标识了一个独立应用程序的包,该应用程序具有客户端应用程序绑定到的绑定服务。


0

感谢CommonsWare!您的建议帮助解决了问题。

为了在AndroidManifest.xml中使用查询,必须升级以下内容:

  1. 将AndroidStudio升级到4.1.3(之前是3.x.x)
  2. 将Gradle版本升级到6.5(gradle-wrapper.properties): distributionUrl=https://services.gradle.org/distributions/gradle-6.5-all.zip
  3. 将Android gradle插件升级到4.1.0(项目build.gradle) classpath 'com.android.tools.build:gradle:4.1.0'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接