Android AIDL-在动态意图上调用BindService将返回null Binder

Android AIDL-在动态意图上调用BindService将返回null Binder,android,aidl,Android,Aidl,我已经创建了一组应用程序。其中一个应用程序基本上是一个“主屏幕”,其他应用程序都包含我创建的一系列服务 interface IExService { void execute(in Bundle parameters, in ICallback callback); } 在主应用程序活动中,我有一个ListView,它使用ArrayAdapter显示我创建的、安装在手机上的自定义服务的类名和包名列表。显示的每个服务都托管在不同的应用程序中,但所有这些服务都实现了我创建的相同的简单AIDL接口

我已经创建了一组应用程序。其中一个应用程序基本上是一个“主屏幕”,其他应用程序都包含我创建的一系列服务

interface IExService {
void execute(in Bundle parameters, in ICallback callback);
}
在主应用程序活动中,我有一个ListView,它使用ArrayAdapter显示我创建的、安装在手机上的自定义服务的类名和包名列表。显示的每个服务都托管在不同的应用程序中,但所有这些服务都实现了我创建的相同的简单AIDL接口

interface IExService {
void execute(in Bundle parameters, in ICallback callback);
}
在主应用程序中,我有一个活页夹和服务连接,如下所示:

private IExService _service=null;

private ServiceConnection _serviceConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder binder) {
        _service=IExService.Stub.asInterface(binder);
    }
    public void onServiceDisconnected(ComponentName className) {
        _service=null;
    }
};
在列表中选择一项后,我想绑定到所选的服务。用于在列表中选择项目的代码如下所示,其中MyService对象表示所选服务项目:

private void executeService(MyService service) {

    boolean isBound = bindService(new Intent(service.getClassName()), _serviceConnection, Context.BIND_AUTO_CREATE);
_service.execute(params, callback);
在上面的代码中,意图将由所选项目持有的类名形成。例如,类名可能看起来像“com.example.exWidgetService.ExampleService”。此服务将按以下方式在该应用程序的清单文件中定义:

    <service android:name="com.example.ExampleApplication.ExampleService">
        <intent-filter>
            <action android:name="com.test.HomeApplication.IExService"/>
        </intent-filter>
    </service>

其中ExampleApplication是我创建的承载服务的应用程序,HomeApplication是定义实际AIDL文件的主菜单应用程序

我的问题是:在调用BindService()时,调用成功,返回true表示服务已绑定。但是_服务对象始终为空。因此,即使服务绑定成功(或返回成功),我也无法执行任何服务方法,因为绑定为null。DDMS似乎没有显示任何有用的内容,并且在BindService()调用期间似乎没有记录任何问题


我有多个应用程序实现同一个AIDL服务,这是一个问题吗?如果没有,是否有合适的方法在运行时动态调用特定的服务实现

如果没有完整的代码和调试,很难说;),让我们一步一步走。 请你核实两件事:

  • 在onServiceConnected方法中,binder参数也为null,还是只有_服务为null? 如果活页夹为空:
  • 在您的服务中,您可以覆盖onBind方法(不记得确切的名称),并检查您是否真的收到了请求

感谢您的回复!onServiceConnected确实返回正确的绑定器参数,并且_服务得到正确设置。这说明了我的问题:onServiceConnected是一个回调。当我调用onBind并立即执行服务方法时,回调还没有被调用。这就是为什么_服务为空。看起来我的解决方案将是插入某种延迟或其他东西,以确保在通过回调进行任何服务调用之前,已到达回调并且_服务已正确设置。谢谢你的帮助!