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

时间:2011-03-18 13:54:36

标签: android aidl

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

在主应用程序Activity中,我有一个使用ArrayAdapter的ListView,我用它来显示我创建的自定义服务的类和包名称列表,这些服务安装在手机上。每个显示的服务都托管在不同的应用程序中,但所有这些服务都实现了我创建的相同的简单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。但是,_service对象始终为NULL。因此,即使服务绑定成功(或返回),我也无法执行任何服务方法,因为绑定器为空。 DDMS似乎没有显示任何有用的信息,并且在BindService()调用期间似乎没有记录任何问题。

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

1 个答案:

答案 0 :(得分:0)

很难说没有完整的代码和调试;),所以让我们一步一步走。 你能否验证两件事:

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