David Cruz

Android: IntentService or JobScheduler?. Use JobIntentService instead (Part 1)

Image for post
Image for post
Source: https://andro4all.com/files/2018/01/android-tired-700x394.jpg

On Android from the API level 3, when we run long background operation we did use IntentService allowing us to run heavy operations on the background without interfering or blocking the UI. However, with the new background execution limits imposed from Android Oreo (API 26) to prevent battery draining, Google introduce the new JobScheduler which intend to replace IntentService in the future, (No so far future as is announced IntentService will be deprecated from Android R — API 30).

The main issue with JobScheduler is, it do not has backwards compatibility, and if you are supporting API below 26, then JobScheduler will not execute. Do not be afraid, as you have 2 ways to solve this issue, first one is handling the API and use IntentService for API < 26 and JobSchedule for API ≥ 26, this solution it requires maintains 2 times same code in order to 1 specific operation (I do personally not recommend it), Or use JobIntentService which it will run in the background as IntentService for API < 26, and as a JobSchedule for API ≥ 26.

How to face and solve this issue?

Well, If you are creating a new JobIntentService, then you can easily follow the steps from the official documentation, basically is copy-paste the code included in to the this url: Link

If you have an IntentService already implemented into your Android Project you can follow this 4 simple steps to upgrade your code to JobIntentService:

First Step:
Modify your extended class from IntentService to JobIntentService and remove the string name inside the constructor

Second Step:
Now you will notice some error code appearing. Do not worry, we will fix it right now.
You will notice an error on the override function onHandleIntent, That is because the function it has change to onHandleWork and the intent as parameter is no nullable anymore, so please, in case you are using Kotlin, remove the question mark to make it no nullable.

Third Step:
Please first of all, create a constant with an Integer number in it, the number can be anyone you chose.

Now is time to replace our context.startService(intent) for function required on JobIntentService. link

The values we send as parameters in our new enqueueWork function is:
* In the First parameter we send the context.
* In the second parameter, we send the our Class set as JobIntentService.
* In the third parameter, we send the constant Integer number we just create.
* And, In the fourth parameter (last), we send the intent.

Fourth Step:
We will add a specific permission into our Service tag in the AndroidManifest.xml:

Following this simple steps, you should be able to merge all your IntentServices to JobIntentServices without affecting your current implementation, and of course, ready to support long background operations from all API levels ≥ 3.

On the part 2, we will see how to use our use multi-threading to use the data received in this JobIntentService from the background thread, move it to the Main thread and display data into our UI using Coroutines.

Happy coding!,
David Cruz

Contact me: https://www.davidcruz.co.uk/contact
My web: https://www.davidcruz.co.uk/
GitHub: https://github.com/DavidCruzUK
Instagram: https://www.instagram.com/dacran/
Twitter: https://twitter.com/davidcruzuk
Youtube: https://youtube.com/c/DavidCruzAnaya
Support me! https://www.patreon.com/davidcruzuk

Written by

Android Lead, Kotlin developer & Blogger

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store