Android:Service Activity Start and Stop Control
Hel开发者_C百科lo Android Gurus For API Level 7 and Above--> I am badly struck trying to figure out a solution for the following problem:
I have an activity which i would use to Kick start an Service. This is an infinite loop service which can run forever. I would like to disconnect the Service from Activity and at a later point of time i will call the activity again which should tell me the current state of Service and i can Stop the service.
Is something like this possible. Right now my Service is Sticky and i am not able to Close the activity as Service is running for ever and i am also not able to Stop the service from activity.
Code samples would be of great help!!! Thanks in advance...
Multiple questions:
You can disconnect/reconnect from the Service using
bind
. Unfortunately the best advice to give there is to carefully read the documentation for Service at http://developer.android.com/reference/android/app/Service.html: look carefully at the Local Service example, as it demonstrates what you need to do to bind/unbind to a sticky service.To stop it, once you've bound you can call stopSelf.
Start the service non sticky from the Activity.
In the service control the flow logically, say calling onStart()
to keep it running.
Call onDestroy()
when you have to stop.
精彩评论