Lifecycle of a Service
The services lifecycle has the following callbacks
Executed when the service is first created in order to set up the initial configurations you might need. This method is executed only if the service is not already running.
Executed every time
startService() is invoked by another component, like an Activity or a BroadcastReceiver. When you use this method, the Service will run until you call
stopService(). Note that regardless of how many times you call
onStartCommand(), the methods
stopService() must be invoked only once in order to stop the service.
Executed when a component calls
bindService() and returns an instance of IBInder, providing a communication channel to the Service. A call to
bindService() will keep the service running as long as there are clients bound to it.
Executed when the service is no longer in use and allows for disposal of resources that have been allocated.
It is important to note that during the lifecycle of a service other callbacks might be invoked such as