Download the New LearnVern Android App and enter Lucky Draw to win Free Certificate
Services in Android
In today's video our topic is Services in Android.
Android services are used when we need to perform long running operations. While running this operation our main activity or application should not stop working.
Application and the service at the background should work simultaneously. Services doesn't have any user interface. It supports in completing the specific task.
In Basic Android we have seen that there are two types of life cycle in Android service, Bound service and Unbound service.
Now we will see what is Started service. A service is started when component like activity calls startService() method and it runs in the background. It is stopped by stopService() method.
The life cycle of unbounded service or started service works as,
Application component-->startService()-->onCreate()-->onStartCommand()-->onStopCommand()-->onDestroy()-->Service shutdown.
Now we will see what is Bound service. A service is Bound when another component calls bindService() method. The client can unbind the service by calling unbindService() method. The service cannot be stopped until all clients unbind the service.
The life cycle of bounded service works as,
Call to bindService()-->onCreate()-->onBind()-->Clients are bound to service-->All clients unbind by calling unbindService()-->onUnbind()-->onDestroy()-->Service shutdown.
User starts a service to attain a task in bound service. Till the task is not achieved the user will bind the service. Once the task is completed the service will be destroyed.
The main difference between bound and unbound service is that Unbound service is used to perform long repetitive task while bounded service is used to perform background task in bound with another component.
Have a friend to whom you would want to share this course?