Leon van Noord Leon van Noord - 5 months ago 29
Android Question

onServiceConnected() not called

I have a problem with using a background service.

I am using the service from 2 activities.

The first activity starts the

Service
with
startService(intent)
and the binds to it with
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);


This works perfectly and I can send a
Message
in
onServiceConnected()
.

The second activity only binds to the
Service
(since it's already started), again using
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);


Now here is my problem:

In the second activity I have a
Button
which should use the service when I press it.

However, the
onServiceConnected()
is never called in this activity, so I can not get the binder to create a
Messenger
to send a
Message
to the server.

Does anyone know when
onServiceConnected()
is called exactly, or what it is possibly waiting for?

Or maybe something else to fix this problem?

EDIT:
bindService
returns false, what could cause this?

Thanks in advance

Answer

After some more research, I discovered this is a known issue in Android.

The second activity I was talking about was an activity which is used as content within a TabActivity.

The way to fix this was to call bindService(...) on the application context, instead of on the activity context using getApplicationContext().bindService(...)

Comments