2017-02-25

Service is the most important component in android. Service is running in background It means that it does not have user interface. It is an independent of activity means it does not matter if activity is running or destroyed, service always running in background. Service is used for long background task it could be any thing.

There are two types of service.
1. Started service or unbound service
2. Bound service.

Started service

Started service are those service that started by Activity or broadcast receiver client and it is independent of activity. It will always be running in background. Service only killed by Android resource when any thing happen wrong for example memory is not available or not enough memory to execute the task.

There are two types of started service.

1. Which extends with Service Class
2. Which extends with IntentService Class.

Lets see what are the basic difference between these two services and where we can used this services?

Service

Service which is running by main thread it should not be any long operation kind of task. But it does not matter, Here you can create your worker thread also for any long ruining task.

Service will continue running in background it does not matter it is completed the task or not, that why it is cause of draining battery. It will only stop when you need to call the stopSelf() method on particular task Id.  For every task it contain one task Id. You need to stop the service once it task has completed. So in case you case avoid battery draining.

It you have started multiple task in side the service then it will parallel running all task. We have not sure which task will finish first and which one in last. Whenever you start your service either from Activity or Broadcast Receiver its life cycle will start executing.

onCreate() -> onStartCommand() -> onDestroy()

onCreate() called means service is created, And after onStartCommand() called it means service running in background. Here you can execute your task. onDestroy() called means your service has finished or killed.

As we above we discussed that your service will destroyed or killed by Android OS if any thing happened wrong like low memory or memory is not enough to execute the task. But we don't want some time our service is stop, we want it will again restart when memory comes available. In this case we can make our service as per our requirement. Service provides option for you to make accordingly. You can set the flag.

1. START_STICKY - When you set your service is start sticky it means it will restart your service again by Android when memory is available to execute the task. But before make sure that you do not passing any intent data because intent data will be lost when service is killed. So we will receive null intent data on startCommand().

2. START_NOT_STICKY - When you set the flag start not sticky then it will not start your service if it killed by android resource and some more flag are available you can check on official developer site. You can set your flag as below.

This is all about the service. Lets check about intent service.

IntentService

Intent service is running by worker thread not by main thread or UI thread. You can not update any UI here because it always running by background thread. It  can used for long operation kind of task for example for downloading files from server etc.

It can also start service from activity or broadcast receiver. When you start service from here its life cycle method will be executed.
onCreate() -> onHandleIntent() -> onDestroy().

Service is created once called the onCreate() and after it will running on onHandleIntent(). Here you can do some task. Service will destroy it will called onDestroy() method.
Intent service automatically stopped once it will complete the task.

If you have multiple task running it will handle one task at one time and other tasks will be in queue.
Once the current task is completed then it will take next task from the queue.

Bound service

Bound service is one that allow to bind service with client. This service will continue running until the client unbind it. This service will start running when it bind by bindService(). This kind of service provide option of client with either in same process or different process. For the different process it bind remotely by IPC (inter process communication).

Inter process communication provides to run service in separate thread or multiple thread. For single thread process it will execute by Messenger and for multiple thread it will handle by AIDL (Android Interface Definition Language)  for example audio, video playing etc.

Lets first check How it work bind service in application. Service can bind either from activity or broadcast. What you need to do just call bindService(). For binding the service You need to pass the IBinder instance in onBind() life cycle of service. When ever your task has copleted then unBind() serrvice from client. Here is example to understand the concept.

Lets create an  activity to bind this service and access this method inside the client.

Here is required ServiceConnection instance to create the connection between client and server. You can bind the service in onStart() method and unbndService() onStop() method. But its per your requirement When we can unbind service.

In my next tutorial I will focus on IPC Remote connection for bind service for single thread and multiple thread. Here is link for Service in android part2 (Coming soon).

Thanks for reading this post.

Show more