0

我有一组我创建的应用程序。其中一个应用程序是一个基本上是“主屏幕”的 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);

在上面的代码中,intent 将由所选项目持有的类名形成。例如,类名可能看起来像“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 服务是否有问题?如果没有,是否有适当的方法在运行时动态调用特定的服务实现?

4

1 回答 1

0

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

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