Thursday, February 9, 2012

What is Service LifeCycle in android?


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.

onResumeonPause, and onStop are not needed
Recall that a service generally has no user interface, so there isn't any need for the onPauseonResume, or onStop methods. Whenever a service is running, it is always in the background.

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.464: E/MyService--onCreate()(425): MyService--onCreate()----onCreate() is called if service is not running else always followed by onStart() and onStartCommand()
09-11 05:51:45.534: E/MyService--onStartCommand()(425): MyService--onStartCommand()

If Only Stop the Service...then

09-11 05:52:55.916: E/MyService--onDestroy()(425): MyService--onDestroy()

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.714: E/MyService--onCreate()(425): MyService--onCreate()
09-11 05:53:47.784: E/MyService--onBind()(425): MyService--onBind()

if directly Unbind then

09-11 05:54:21.654: E/MyService--onUnbind()(425): MyService--onUnbind()
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.355: E/MyService--onCreate()(425): MyService--onCreate()
09-11 05:55:30.425: E/MyService--onStartCommand()(425): MyService--onStartCommand()
After some time on some event if bind is done then
09-11 05:55:31.435: E/MyService--onBind()(425): MyService--onBind()


If Unbind but doesn't stop the service...then

09-11 05:56:44.394: E/MyService--onUnbind()(425): MyService--onUnbind()

After Inbind stop the Service...then
09-11 05:57:32.829: E/MyService--onDestroy()(425): MyService--onDestroy()

1 comment: