Saturday 14 June 2014

Android Services


Service: An Android Component


One of the core component of Android Application, others are Activity, Broadcastreceiver and Content Providers. It performs long-running operations in the background and does not provide a user interface. It is different from the activity component that has a user interface. It also does not destroy when you rotate the device from portrait mode to landscape mode like activity does. A service still runs in the background while user is not interacting with the application i.e application is not visible to the user. It perform background operation to support other component like activity and broadcastreceiver. Application like music player, always need to play the music even though application is not visible or partial visible. Same can be said for a download application. These requirements can be achieved via using a thread inside the activity class. But, the thread life is associated with the activity life cycle, so whenever activity is recreated the thread will also goes off. This way a thread can’t provide a mechanism to continue the operation.


Android Service Features


A service is hosted by the same process which start the launcher activity and runs on the main UI thread only. In this way it is not a separate process. Also since it runs on the same UI thread, it is not a thread. Service are very useful android component for long running process which has its own lifecycle. It complements the activity android component to achieve the desired goal by providing background support.


A service provides Inter process communication mechanism among different processes for the same application or different applications.


Since it also runs on the UI thread, for better user experience create a separate thread inside the service for long running process.


Service Examples
Some of the use cases where a service component can be very useful and efficient are listed below.


To perform network transaction
A network operation like downloading a file from a server like audio or video or even a picture, your network may be bad at times or internet speed may be slow. So in this scenario the operation must be performed on a background thread. Also android design does not allow you to perform any network operation on its main UI thread. This thread can be created inside the download service component.


Play Music
A music player application must be run even it is not visible to the user. So a service component becomes an essential part of this kind of the application. It will run forever till user interrupt to stop it. In this way it is a long running process should be performed by a service.


Interact with content providers
Content provider is another android component that provides data to the other android application. Some of the android provided content providers are Contacts, Calendar, Dictionary etc. These are available to all the other android application. But these content provider have to deal with database query which is an time consuming and expansive process. So these operation should be performed in the background over a separate thread inside a service and obviously this is a background process, user does not need to know about it.


Syncing with the cloud
Some data need to be synchronized before presenting to the application. This synchronization of data can be happened over a cloud where data backup is residing. So to connect the cloud and perform synchronization operation take time. So it should be performed in the background and using a service only.


Types of service inside Android


Till now, we have discussed about the service in general. Now let us dive into service development inside the android application. Android API provides two types of services, they call them as “Started Service” and “Bound Service”. In the below table, I have mentioned some of the key differences between these two services.


“Started” Service
“Bound” Service
  • Used within the same process
  • Run indefinitely
  • Should perform a single operation
  • Does not return a result to caller
  • Used within as well as across the processes address space.
  • It is client server interface
  • Send requests and get results
  • Runs until any component is bound to the service


Started Service


This kind of the services are those services which are started by the other component of the android via method startService() . This cane started from an activity or from a broadcastreceiver or from a content provider. Once the service is started, it will run till the call of selfStop() or stop() by the caller component. This service can also be destroyed by the system in case of low memory.


Android provides two ways to create a started service. One is simply extends IntentService class and other is by extending the Service class. These types of services will be discussed in details in later posts. This service is generally used within the same process by the other component and not in other process or by other applications. This service is very suitable for a single operation like downloading a file from internet or sending some files over a wifi network in a LAN environment or over WIFI-Direct use cases. Generally caller does not need a result back to it.


Bound Service
Bound services are those service which has lifetime till the client is bound to it. If any client that is currently bound to this kind of the service, this service will alive and do the service to the client. It basically provides a client server architecture over which communication happens between the caller component and the service component. Once all the client become unbound to it, the service will be destroyed by the system.


This kind of the service is created when the caller component call method bindService() and not startService() method. This services can be used across the process boundary and within the same process also. Bound service can be developed via the following three ways


By Extending Binder class
If you need to implement the bound service that will be used within the same process then you can simply extends the Binder class. This is also useful for a single operation to be performed for a request from the client. The client can be another component of the same process.
By using Messenger class
If you want to use the service across the other process then you should create the service using messenger class. In this kind of the service, multiple requests from the clients are served sequentially using a single thread within the service.


Using AIDL
To enhance the multi threading capabilities inside your service and to give this service to the other process or application, you need to implement the service using AIDL ie. Android Interface Definition Language. In this service you can use multiple threads to service multiple clients simultaneously.


The diagram below shows the relation about accessing different services by components living in different address spaces.




In the above diagram, I have depicted all kind of services available in the android system and there communication with the other components. A started service can’t cross its own process boundary. So you can call startService() from the other application process.


For bound service class, you can cross the application boundary. Bound services can be served to the other application from the serving application. This is also shown in the picture. For multi requests there are two options for using thread.


No comments:

Post a Comment