【问题标题】:android remote service running in a separate process and call methods from onserviceconnected()android 远程服务在单独的进程中运行并从 onserviceconnected() 调用方法
【发布时间】:2013-10-31 20:44:44
【问题描述】:

我正在编写一个使用 AIDL 远程调用方法的服务。我能够成功连接到该服务,并且还可以在其上调用方法。但是,如果我尝试调用 onServiceConnected() 中的方法,则会出现空指针异常。代码片段如下。

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
 Log.v(TAG, "service connected");
 serviceObj = <AIDL Interface>.Stub.asInterface(service)
 if(serviceObj != null) {
   serviceObj.getMyMethod(); // Throws Null Pointer Exception, why???
} 
}

另外,在清单中,我在服务标签中定义了以下属性:

android:process=":remote"

但是,如果我在其他地方调用 serviceObj.getMyMethods(),例如单击 GUI 按钮,则不会收到此类错误,并且方法会成功返回。另外,如果我删除上面的 ":remote" 属性,同样的代码我不会得到这个错误。

我猜这是与在不同进程中运行应用程序和服务相关的问题,但无法找出确切原因。有什么想法吗?

【问题讨论】:

    标签: java service android-service aidl


    【解决方案1】:

    我终于想通了。由于 Context.startService() 是异步调用,因此发生了一些内部崩溃/空指针异常。当 onStartCommand() 正在执行一些任务时,在 serviceObj 上并行调用的方法崩溃了,因为它需要首先进行一些初始化,而这发生在 onStartCommand() 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 2023-03-24
      • 2011-06-07
      相关资源
      最近更新 更多