Service vs IntentService in Android

In our day to day life, we all come across a number of mobile application that runs in the background. Also, in many applications, certain tasks are being performed without using any UI i.e. the task is being performed in the background. For example, the Music app of our mobile device or any other Music application runs in the background and while using the Music app, you can use any other application normally. So, this feature is implemented using the Service or IntentService.

In this blog, we will look upon the differences between the Service and IntentService. But before moving forward, we will have a quick revision about Service and IntentService. So, let’s get started.

Service

You can think Service as an Android component that is used to perform some long-running operations in the background like that in the Music app, where we run the app in the background and use other applications of the mobile parallelly. The best part is that you don’t need to provide some UI for the operations to be performed in the background. By using Service, you can perform some InterProcess Communication(IPC) also. So, with the help of Service, you can perform a number of operations together because any application component can start a Service and can run in background.

There are three ways of using Service:

  1. Foreground: A foreground service is a Service that will let the user know about what is happening in the background. For example, in the Music application, the user can see the ongoing song on the device as a form of notification. So, here displaying notification is a must.
  2. Background: Here, the user will never know about what is happening in the background of the application. For example, while sending some images over Whatsapp, Whatsapp compresses the image file to reduce the size. This task is done in background and the user have no idea about what is going in the background. But for the API level 21 or higher, the Android System imposes some restrictions while using the Background Service. So, take care of those restrictions before using the Background Service.
  3. Bound: The Bound Service is used when one or more than one application component binds the Service by using the bindService() method. If the applications unbind the Service, then the Service will be destroyed.

IntentService

The Service is the base class for the IntentService. Basically, it uses “work queue process” pattern where the IntentService handles the on-demand requests (expressed as Intents) of clients. So, whenever a client sends a request then the Service will be started and after handling each and every Intent, the Service will be stopped. Clients can send the request to start a Service by using Context.startService(Intent). Here, a worker thread is created and all requests are handled using the worker thread but at a time, only one request will be processed.

To use IntentService you have to extend the IntentService and implement the onHandleIntent(android.content.Intent).

Difference between Service and IntentService

So, we have revised the concept of Service and IntentService. Till now, have you found any difference between these two? If not, don’t worry, in this section, we will look upon some of the differences between the Service and IntentService, so that it will be easier for you to find which one to use in which condition. Let’s see the difference, point wise:

  1. Usage: If you want some background task to be performed for a very long period of time, then you should use the IntentService. But at the same time, you should take care that there is no or very less communication with the main thread. If the communication is required then you can use main thread handler or broadcast intents. You can use Service for the tasks that don’t require any UI and also it is not a very long running task.
  2. How to start?: To start a Service you need to call the onStartService() method while in order to start a start IntentService you have to use Intent i.e. start the IntentService by calling Context.startService(Intent).
  3. Running Thread: Service always runs on the Main thread while the IntentService runs on a separate Worker thread that is triggered from the Main thread.
  4. Triggering Thread: Service can be triggered from any thread while the IntentService can be triggered only from the Main thread i.e. firstly, the Intent is received on the Main thread and after that, the Worker thread will be executed.
  5. Main Thread blocking: If you are using Service then there are chances that your Main thread will be blocked because Service runs on the Main thread. But, in case of IntentService, there is no involvement of the Main thread. Here, the tasks are performed in the form of Queue i.e. on the First Come First Serve basis.
  6. Stop Service: If you are using Service, then you have to stop the Service after using it otherwise the Service will be there for an infinite period of time i.e. until your phone is in normal state. So, to stop a Service you have to use stopService() or stopSelf(). But in the case of IntentService, there is no need of stopping the Service because the Service will be automatically stopped once the work is done.
  7. Interaction with the UI: If you are using IntentService, then you will find it difficult to interact with the UI of the application. If you want to out some result of the IntentService in your UI, then you have to take help of some Activity.

Conclusion

In this blog, we learned some of the differences between the Service and IntentService in Android. We discussed some of the ways of starting and stopping the Service and IntentService. If you some limited amount of tasks to be performed in the background, then you can use Service, otherwise, you can use IntentService.

You can refer to the Android official documentation for the best practice on Background Services.

Keep Learning :)

Team MindOrks!