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.
Thanks in advance
I can't make up the exact problem out of your description, so I'm going to guess here!
How can
bindService()
throw aNullPointerException
? The only way this could (/should) happen is when you don't supply aService
or aServiceConnection
listener.bindService()
can't throw aNullPointerException
becauseonServiceConnected()
isn't called. The call toonServiceConnected()
is a product ofbindService()
.So I guess you are calling a
AIDL
method, before theService
has actually bonded?The onServiceConnected event was never being called in my application.
The problem was that I had the service name defined in my Application Manifest as:
Once I changed it to the full class name it worked:
Update: You can also have use a relative class name:
Specify the class name using its full com.example.MyServiceClass instead of just MyServiceClass.
After hours and hours of trying to figure this out, the issue is that the examples that show the creation of the service, don't include the onBind method or they have the following sample code or it generates this for you:
This causes the onServiceConnected method to fail or never actually get executed. The fix is VERY simple, which is the following:
Where you could create a simple binder such as the following to return:
I was calling bind with an empty Intent -
getContext().bindService(new Intent(), mConnection, Context.BIND_AUTO_CREATE)
. I have to make the intent more specific to indicate which service I want to bind to. This is obviously a code error, but the Logcat output was unfortunately not clear enough.Yet another cause to the original question might be that the service isn't already running and you are passing 0 as flag to bindService. Like so:
When what you are looking for would be:
I've just experienced another version of this problem, with the same symptom of
onServiceConnected(...)
not being called. The cause was different in my case.You must make sure to have a service declaration in your AndroidManifest.xml within the application tag - this was the root of the problem for me.
There's an extra complication if you're using a separate Android library within Eclipse - adding this Service tag only seems to fix the issue if the referenced service is in the same package as the manifest; i.e. if your app is in package a.b.c and this is where AndroidManifest.xml resides, then 'YourService' must also be in package a.b.c. (manually copied from another library, if necessary) or else the
<service..>
tag may/will be ignored andonServiceConnected(...)
still won't be called.This was the case for my project even though I used a suitable import statement for the Service in my code. Eclipse showed no error, so the import was correctly identifying the class from another library in the Eclipse workspace.
HTH