Android Service Lifecycle
The lifecycle for a service is similar to that for an activity, but different in a few important details:
onCreate and onStart differences- Services can be started when a client calls the Context.startService(Intent) method. If the service isn't already running, Android starts it and calls its onCreate method followed by the onStart method. If the service is already running, its onStart method is invoked again with the new intent. So it's quite possible and normal for a service's onStart method to be called repeatedly in a single run of the service.
onResume, onPause, and onStop are not needed
onBind- If a client needs a persistent connection to a service, it can call the Context.bindService method. This creates the service if it is not running, and calls onCreate but not onStart. Instead, the onBindmethod is called with the client's intent, and it returns an IBind object that the client can use to make further calls to the service. It's quite normal for a service to have clients starting it and clients bound to it at the same time.
onDestroy- As with an activity, the onDestroy method is called when the service is about to be terminated. Android will terminate a service when there are no more clients starting or bound to it. As with activities, Android may also terminate a service when memory is getting low. If that happens, Android will attempt to restart the service when the memory pressure passes, so if your service needs to store persistent information for that restart, it's best to do so in the onStart method.
If Only Service is Started...then
09-11 05:51:45.534: E/MyService--onStartCommand()(425): MyService--onStartCommand()
If Only Stop the Service...then
If directly Bind the Service...then...Here the onStart method will never get call in both state(service running or vice-versa)
09-11 05:53:47.784: E/MyService--onBind()(425): MyService--onBind()
if directly Unbind then
09-11 05:54:21.765: E/MyService--onDestroy()(425): MyService--onDestroy()
If started the Service and Bind it...then
09-11 05:55:30.425: E/MyService--onStartCommand()(425): MyService--onStartCommand()
After some time on some event if bind is done then09-11 05:55:31.435: E/MyService--onBind()(425): MyService--onBind()
If Unbind but doesn't stop the service...then
After Inbind stop the Service...then09-11 05:57:32.829: E/MyService--onDestroy()(425): MyService--onDestroy()
Useful Information. Get more on Android Online Training
ReplyDelete