I'm starting a service in my application using startService.
I do not want to use bindService as I want to handle the service life time myself.
How can I get an instance to the service started if I do not use bindService? I want to be able to get a handler I've created in the service class to post messages from the activity.
Thanks.
/ Henrik
That does not mean you have to avoid
bindService()
. Use bothstartService()
andbindService()
, if needed.Either use
bindService()
withstartService()
, or use a singleton.Here's another approach:
onServiceConnected(...) can cast its argument to
MyService.Binder
and callgetService()
on it. This avoids the potential memory leak from having a static reference to the service. Of course, you still have to make sure your activity isn't hanging onto a reference.