Need to know this thing before changing IntentService to JobIntentService in Android?

shubham hupare
3 min readDec 2, 2018

--

What is JobIntentService?

Helper for processing work that has been enqueued for a job/service. When running on Android O or later, the work will be dispatched as a job via JobScheduler.enqueue. When running on older versions of the platform, it will use Context.startService.

One of Android’s greatest strengths is its ability to use system resources in the background regardless of app execution. Sometimes it became the behavior to use system resources excessively.

Checkout Background Execution Limits in Android O

Let’s Go

IntentService.class is an extensively used service class in Android because of its simplicity and robust in nature. Since the release of Oreo, made things more difficult for developers to use this class in a full swing for their applications. For those who are relied on IntentService class, from oreo onwards you cannot simply use this class. We are also searching for the alternative of IntentService. Finally, our search ends with the JobIntentService which exactly does the same job of IntentService by using new Job APIs of oreo. This class is available in the support library of SDK 26.

You can easily migrate from IntentService to JobIntentService.

https://android.jlelse.eu/keep-those-background-services-working-when-targeting-android-oreo-sdk-26-cbf6cc2bdb7f.

For device targeting SDK 26 or later, This class’ works are dispatched via JobScheduler class and for SDK 25 or below devices, It uses Context.startService() (Same as in IntentService).

While changing IntentService to JobIntentService I used to read the number of articles. All articles that I read, only mention What is JobIntentService? Why we should use it instead of IntentService? How to implement it? I have made one demo project which contains an Activity which has a button. On the button click, I have made an API request using a service. I tested various scenarios on my code(using IntentService and JobIntentService as well) by considering different background execution limits.

In all scenarios, JobIntentService works exactly the same as that of the IntentService except one scenario.

I killed my application by swapping it from recent apps when onHandleIntent()/onHandleWork() callback is in progress (Created this scenario by inserting following code at the start of onHandleIntent()/onHabdleWork() and killed app while loop is in progress).

for(int iterator = 0 ; iterator < 10 ; iterator++) {
Thread.sleep(1000);
Log.d(TAG, "onHandleIntent() : " + iterator);
}

For IntentService:

As soon as an application gets killed, loop inside onHandleIntent() also stops. You will not see any further logs which incrementing your loop count in your debug logcat. It means when you are using IntentService once your application gets killed onHandleIntent() never execute till you will not start service again.

For JobIntentService:

After an application gets killed, after few seconds loop inside onHandleWork() starts executing but this time it starts from 0 loop count. It means with JobIntentService if your application gets killed, still your task will be get executed using onHandleWork() by restart its execution.

So now let’s consider you have an application which has code to save profile picture that you have uploaded by making some API request.

With IntentService, if your uploading is in progress and your app gets killed, your profile picture will not save to the server but now JobIntentService will save it.

So now, in this case, it was an advantage but there are some cases where we don’t want to perform a task when our application does not exist. In this case, we have to be very careful while converting IntentService to JobIntentService.

This was my observations while using JobIntentService. I hope this article will be helpful to those who want to change their code to use JobIntentServicce instead of IntentService.

Thank you.

--

--

shubham hupare
shubham hupare

Written by shubham hupare

An enthusiastic Android developer who likes to deal with new technologies and implement it.

Responses (4)