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

davthecoder
3 min readNov 14, 2020
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).

(Source: https://developer.android.com/reference/android/app/IntentService)

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

// Old code:
class MyIntentService : IntentService("MyIntentService") { ...
// New Code
class MyIntentService : JobIntentService() { ...

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.

// Old code
override fun onHandleIntent(intent: Intent?) {
// New Code
override fun onHandleWork(intent: Intent) {

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

const val INTENT_SERVICE_ID = 1003

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

// Old code
context.startService(intent)
// New code
enqueueWork(context, MyIntentService::class.java, INTENT_SERVICE_ID, intent)

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:

// Permission to add
android:permission="android.permission.BIND_JOB_SERVICE"
// Old code
<service
android:name=".MyIntentService"
android:exported="false" />
// New code
<service
android:name=".MyIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false" />

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.

Source: https://i.kym-cdn.com/entries/icons/original/000/002/686/Deal_with_it_dog_gif.gif

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

Let’s Keep in touch:
davthecoder Web: https://www.davthecoder.com
Medium Blog: https://davthecoder.medium.com/
Twitch: https://www.twitch.tv/davthecoder
GitHub: https://github.com/davthecodercom
Instagram: https://www.instagram.com/davthecoder
Twitter: https://twitter.com/davthecoder
Youtube: https://youtube.com/@davthecoder
Support me on Patreon! https://www.patreon.com/davthecoder

--

--