- What is Service in android?
The Service is an application component in android Operating System. It is used to perform long-running operations in the background even when the application is not running. It functions as like an Activity but does not contain any user interface.
User’s may like to listen music in the background when he is browsing internet, may be playing a game when downloading the attachment from the email. If you are the developer of that applications, you should use the service.
- What are the purposes of Service?
This is the most important question in Android interview questions. The Service is used to perform long running operations out of the UI thread. Long running operations like- Downloading files from internet, periodic data synchronization, working with file I/O, fetching phone contact or SMS etc. should be performed in the Service to avoid UI lags and to provide better user experience.
We are so familiar with the push notifications by the application of social media, email, news portal, weather etc. These applications are also using service in the background to synchronize new feeds from the internet to notify us.
- What are the basic differences between bound and unbound service?
After starting a service we may not need to communicate with that again in few cases.
For example if we start service to fetch new data from internet and updating the local database, we do not need to communicate with the service when it’s running, because when the service completes synchronization, it can push notification, start an activity or can stop silently.
In this case we can use unbounded service.
But when we start a music in the service we may need to get information of that music file, the current position of the music to update the seekbar in the user interface. In this case we will have to communicate with the service from the activity, when it’s playing music in the background.
The differences between bounded and unbounded service are as follows.
|Bounded Service||Unbounded Service|
|It gets started by calling bindService() method from an application component.||It get started by calling startService() method from an application component.|
|Android component binds to it. This service offers a client-server interface which allows the android component to interact with the service by sending request and getting result.||After getting started, it keeps running in the background indefinitely. The component cannot interact with this anymore.|
|When the caller component is destroyed the service automatically destroys itself.||This service can run in the background after the caller component is destroyed even after closing the application.|
- Explain lifecycle of Service.
There are two types of Android Service. Their lifecycles are also different –
- Bounded Service
onCreate() : This method is called by the system when the service is created for the first time. If the service is already created and other components request again to start the service, this method will not be called again.
onBind() : This method is called by the system when another component wants to bind with the service by calling bindService() method. To implement this method it is required to provide an interface that the client component can communicate with the service by returning an IBinder object. To avoid binding with the service this method should return null.
onUnbind() : This method is called by the system when all of the clients have disconnected from a bounded service.
onRebind() : This method is called by the system when a new application component gets connected with the service after it had previously been notified that it was unbounded from all of the previous clients and onUnbind(Intent) was called.
onDestroy() : This method is called by the system when all of the clients are disconnected from this service and is being destroyed. In this state all of the used resources should be cleaned up such as threads, registered listeners, receivers etc.
(Image courtesy: developer.android.com )
- Unbounded Service
onCreate() : Same as the bounded service.
onStartCommand() : This method is called by the system when an application component sends request to start a service by calling startService(). After the service starts, it’ll run in the background until you stop the service. The service can be stopped by calling stopService() method from an application component or by calling stopSelf() method inside the service. In special case the operating system may stop the running services when the resource capacity is getting exceeded by the process load. But when you start a service it’s your responsibility to stop it.
onDestroy() : Same as the bounded service.
To get more information about the android interview question, click here