Implementing Intent Services in Android — A step by step Guide

Archit Gupta
Code Yoga
Published in
4 min readMay 10, 2018

Intent Services are implemented to handle background running tasks as a service

In this article I will taking an example of Hydration app (taken reference from Udacity Android App DevelopmentCourse) where we are incrementing water count (number of times we have drank wanter) by tapping on the image

Source

This increment can be achieved by using shared preferences which will keep record of the count and is much fast.

But still we are implementing intent services here because it can be required in a case where counter has to be updated on a remote database keeping all our health records. That can be a time consuming task which needs to be done in background because server requests can be time consuming and might outlast activity lifecycle time.

Source

CODE IMPLEMENTATION

Intent Services handle many tasks in the background. So all tasks should be kept in one place. Here we are defining all tasks under a single roof — Reminder task. You can start implementing by creating a helper class to keep code organised and modular. Reminder task will define all tasks running in background for the app. Tasks can be multiple as well as just a single one. In our case we are taking a single task for now — incrementing the water count

Source

TASK EXECUTION

- Activity will give a start to intent service

- Service will execute the increment task

- This task will use PreferenceUtils class to increment counter in shared preferences (Shared Preferences will keep track of counter all the time)

Source

Enough talking right ? :P … Now lets code :B

CODE

→ First, we give name to our first task as an action name — “increment-water-count”. This task will be referred always by its action name. It is the First intent action that reminder class has to handle

→ Create method that performs incrementation of water count — incrementWaterCount. It uses PreferenceUtils class to do that which incements the count stored in Shared Preferences.

→ Finally, create executeTask method which will execute the increment method only if the received action matches the action name we created. After all we have to know which task we need to perform in case of multiple tasks and this method will keep a check on that

Coding the actual intent service

→ Create WaterReminderIntentService class which must extend IntentService. Fill in the constructor with name of service — we take name of class as service here

→ Override inbuilt method — OnhandleIntent. It is the method that intent service calls on background thread. We need to know which action we need to execute so get the action from intent first of all — getAction()

→ Now as we have the task to perform, perform it by calling execute method from our helper class, tell the helper class that this action needs to be performed by sending action name and necessary context

→ As any other android component, service needs to be registered in android manifest. We’ll create a service tag inside the application tag itself. Setting exported attribute as false avoids other apps from using our service class.

→ Everything done, we just need to start the service now and that will be done in main activity

→ We define an intent for our service — incrementWaterCountIntent. Set the action name for it to perform that action — name is taken form our helper class itself where it is defined

Start the service

→ The method incrementWater(inside which we just started our service), is already set to be called at every click on the cup using onClick() method.

So this was a basic example to learn to implement intent services. I will be making articles on many more android topics. Please do share and clap if you like, keep supporting :)

--

--