Android JobIntentService for background task

Kishan Maurya
MindOrks
Published in
3 min readJun 12, 2019
Type of services

Intent Service creates a separate working thread to perform background operation. Once his job is finished is automatically destroyed. In other words, you can say IntentService is advanced background service that creates a separate background thread to perform operations and automatically destroyed when the job is done.

IntentService does not work well in our Oreo devices. On Android O, a background service that will run a few minutes after the app enters the background, and will automatically stop and onDestroy() will be called. In addition, a call to startService() to run the service in the background state is not allowed because an IllegalStateException is thrown.

To overcome this problem android introduced JobIntentService that works well in all the devices. You can say JobIntentService is a modern way to run the background service from the background application.

JobIntentService

JobIntentService works in the same way as a Service however it enqueues the work into the JobScheduler on compatible Android targets( SDK 26 or more).
This means you can easily convert your Services and IntentServices into a JobIntentService and keep the same functionality.

The JobIntentService also exposes a static method to start the service (JobIntentService.enqueueWork()).

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

Few Points regarding JobIntent Service

On Android Oreo devices, it will handle the Wake Locks for you.

On any device pre-Android Oreo, the JobIntentService will set up Wake Locks through the PowerManager, so make sure you require the WAKE_LOCK permission in your manifest.

<uses-permission android:name=”android.permission.WAKE_LOCK” />

When running on anything less than Android Oreo the service will start almost instantly. This ensures backwards compatibility with the same code. On Android Oreo, it will be subject to JobScheduler policies.

On pre-Android Oreo, the service can run indefinitely but on Android Oreo, it will adhere to the usual JobService execution type limits.

In Android O and later, jobs loaded through JobScheduler are executed sequentially. Internally, it is scheduled byjobInfo.setOverrideDeadline(0).build().

How To create JobIntent Service

  1. Create a subclass of JobIntentService
import android.support.v4.app.JobIntentService;public class MyJobIntentService extends JobIntentService {
}

2. Now override the onHandleWork() methods and expose enqueueWork() methods like below.

private static final int JOB_ID = 1;public static void enqueueWork(Context context, Intent intent) {
enqueueWork(context, MyJobIntentService.class, JOB_ID, intent);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
/**
Write code here.. Perform Long operation here such as Download/Upload of file, Sync Some data
The system or framework is already holding a wake lock for us at this point
*/
}

3. Configuration in AndroidManifest.xml file
<uses-permission android:name="android.permission.WAKE_LOCK"/>

4. Call from Activity/ Fragment/Receiver

public void onStartJobIntentService() {
Intent mIntent = new Intent(this, MyJobIntentService.class);
MyJobIntentService.enqueueWork(this, mIntent);
}

Thanks for reading. Soon I will post more articles on android, core java.
till then happy learning… keep reading… :)

--

--