本文介绍了在 bindService 方法之后从未调用 onServiceConnected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特殊情况:由广播接收器启动的服务启动一个活动.我想让此活动能够与服务进行通信.我选择使用 AIDL 使其成为可能.除了在活动的 onCreate() 中调用的 bindService() 方法外,一切似乎都运行良好.事实上,bindService() 会抛出一个空指针异常,因为 onServiceConnected() 永远不会被调用,而服务的 onBind() 方法是.无论如何 bindService() 返回 true.该服务显然处于活动状态,因为它启动了活动.我知道从服务调用 Activity 听起来很奇怪,但不幸的是,这是在服务中进行语音识别的唯一方法.

I have a particular situation:a service started by a broadcast receiver starts an activity. I want to make it possible for this activity to communicate back to the service. I have chosen to use AIDL to make it possible. Everything seems works good except for bindService() method called in onCreate() of the activity. bindService(), in fact, throws a null pointer exception because onServiceConnected() is never called while onBind() method of the service is. Anyway bindService() returns true.The service is obviously active because it starts the activity.I know that calling an activity from a service could sound strange, but unfortunately this is the only way to have speech recognition in a service.

提前致谢

推荐答案

我无法从你的描述中弥补确切的问题,所以我会在这里猜测!

I can't make up the exact problem out of your description, so I'm going to guess here!

bindService() 如何抛出 NullPointerException?这可能(/应该)发生的唯一方法是当您不提供 ServiceServiceConnection 侦听器时.

How can bindService() throw a NullPointerException? The only way this could (/should) happen is when you don't supply a Service or a ServiceConnection listener.

bindService() 不能抛出 NullPointerException 因为 onServiceConnected() 没有被调用.对 onServiceConnected() 的调用是 bindService() 的产物.

bindService() can't throw a NullPointerException because onServiceConnected() isn't called. The call to onServiceConnected() is a product of bindService().

所以我猜您在 Service 实际绑定之前调用了 AIDL 方法?

So I guess you are calling a AIDL method, before the Service has actually bonded?

这篇关于在 bindService 方法之后从未调用 onServiceConnected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 06:29