views:

221

answers:

1

Hello all,

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

+2  A: 

I do not want to use bindService as I want to handle the service life time myself.

That does not mean you have to avoid bindService(). Use both startService() and bindService(), if needed.

How can I get an instance to the service started if I do not use bindService?

Either use bindService() with startService(), or use a singleton.

CommonsWare
By "using a singleton" you mean that I should declare my methods static in the service class?
Henrik
Worked as a charm with both bindService and startService. Thank you!
Henrik
@Henrik: FWIW, by singleton, I meant that you'd have a static reference to your service, put there by the service's `onCreate()`, removed in the service's `onDestroy()`. That runs the risk of memory leaks, so binding is recommended wherever possible.
CommonsWare