If you are looking to learn about IntentService and How to implement it in your app. This is the right place for you. I will be discussing IntentService, how it works, and how to implement in an Android using Kotlin language. General knowledge about Service in Android will be helpful to understand this concept.
A Service is an android component that performs long-running process in the background, and Service does not have a UI component associated with it like an activity. By default, it runs on UI thread or Main thread. It also has lifecycles like an Activity but much simpler. If you start a service, it's your responsible to stop the service, otherwise it will be keep running in the background and draining the phone battery. The system itself will kill it under certain circumstances such as Memory low. In this tutorial I would be primarily discussing IntentService, so if you want to know more about Service please refer here.
An IntentSerive is a subclass of a Service class. The difference between the Service and IntentService is :
- By Default, it runs on Main Thread or UI Thread.
- It’s your responsible to stop it, otherwise it will be keep running in the background. System kills it under certain circumstances.
- By Default, it runs on worker thread. Not the UI thread.
- Once the task is completed or no more task is available. It will terminate itself.
- You can start multiple service, but only one service can run at a time.
- It’s Asynchronous
Let's talk about how to define the IntentService.
You have to create a class (Provide a relevant class name is useful, such as MyIntentService), and inherit IntentService. Make sure you have the primary constructor with a name (String) as an argument, pass name to IntentService, provide a default value for the name and override onHandleIntent(..). Otherwise, it will produce an error in manifest file when you register the service. We will see later how to define that in the Manifest File. An example class definition as follows :
Whenever you start a service the onHandleIntent(p0 : Intent?) will be called with Intent you passed when you started the Service. You can perform the work you want to perform inside this method. You can also override other methods that is available such as onDestroy() — which is called during the destruction of this service. You can a use a log or any sort of indication to indicate the destruction of this service.
We will see later how to start service. We are done with extending the IntentService. Now we will see how to register the service in the Manifest file.
In your Android Manifest inside the <activity.. >tag add a <service> </service>tag. An example below illustrates how to register the Service :
The Service tags name attribute takes the name of your IntentService class you have defined, and it begins with a period. In our case the class name is MyIntentService. If you did not provide a default name for your service in the class definition, the Android Studio will show an error under your class name here. So, we are done with how to registering the service.
Lets start the service…
To start the service, you have to create an Intent object, then pass the context and your IntentService class name, you can pass the name, otherwise the default value will be used. You can pass some data to Intent via bundle. Then you have to call startService(intent) method by passing your intent will start the service. An example below shows this :
You can read the data passed in an Intent in your onHandleIntent(..) and log the data. Each time you start a service, it will log the data on the console.
Like I mentioned earlier, you can start multiple service by creating multiple Intents, and calling startService(..) for each. Only one service is processed at a time. They are non blocking or asynchronous.
If you override onDestroy() method in your IntentService class, and log something to indicate that service is destroyed. At each time when you start a service you can see the service getting destroyed by itself after it’s completed the task. An example of this :
I hope this tutorial was helpful. Please point out if I made any mistake.