A Short Note – Service vs IntentService In Android

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (Service vs IntentService In Android).

In this note series, we will understand the differences between the Service and IntentService in Android.

So Let’s begin.

Service

Service is an android component that is used to perform some long-running operations in the background, such as in the Music app, where we run the app in the background while using other mobile apps at the same time. The best part is that we don’t need to provide some UI for the operations to be performed in the background. By using Service, we can perform some InterProcess Communication(IPC) also. So, with the help of Service, we 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 Service :

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 Service :

Here, the user will never know about what is happening in the background of the application. For example, whatsapp messenger compresses the image file to reduce the size while sending some images over whatsapp. 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, we take care of those restrictions before using the Background Service.

3. Bound Service :

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, we have to extend the IntentService and implement the onHandleIntent(android.content.Intent).

Service vs IntentService

In this section, we will look upon some of the differences between the Service and IntentService, so that it will be easier for us to find which one to use in which condition. Let’s see the difference:

  • If we want some background task to be performed for a very long period of time, then we should use the IntentService. But at the same time, we should take care that there is no or very less communication with the main thread. If the communication is required then we can use main thread handler or broadcast intents. We can use Service for the tasks that don’t require any UI and also it is not a very long running task.
  • To start a Service, we need to call the onStartService() method while in order to start IntentService, we have to use Intent i.e. start the IntentService by calling Context.startService(Intent).
  • Service always runs on the Main thread while the IntentService runs on a separate Worker thread that is triggered from the Main 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.
  • If we are using Service then there are chances that our 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.
  • If we are using Service, then we have to stop the Service after using it otherwise the Service will be there for an infinite period of time i.e. until our phone is in normal state. So, to stop a Service, we 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.
  • If we are using IntentService, then we will find it difficult to interact with the UI of the application. If we want to out some result of the IntentService in our UI, then we have to take help of some Activity.

Conclusion

In this note series, we understood about Service and IntentService differences and usages in android. We also discussed about fundamental concepts of Service and IntentService. If we have some limited amount of tasks to be performed in the background, then we can use Service, otherwise, we can use IntentService.

Thanks for reading! I hope you enjoyed and learned about Service vs IntentService in Android. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

Loading

Leave a Comment