Android Job Scheduler – Scheduling of tasks

by Sreejith N

on July 24, 2018

JobScheduler was introduced in Lollipop, and is pretty cool because it performs work based on conditions, not on time.Job scheduling APIs let developers schedule work to be done when certain conditions are met instead of at a specified time. So rather than scheduling a download for 1am and needing to worry about whether the device is charging or on an expensive data connection, we can just use the JobScheduler and tell it not to start our job until the device is charging and on Wi-Fi.

Our goal with JobScheduler was to find a way for the system to shoulder part of the burden of creating performant apps. As a developer, we do our part to create an app that doesn’t freeze, but that doesn’t always translate to the battery life of the device being healthy. So, by introducing JobScheduler at the system level, we can focus on batching similar work requests together, which results in a noticeable improvement for both battery and memory.

Scheduling Options

If you have a repetitive task in your Android app, you need to consider that activities and services can be terminated by the Android system to free up resources. Therefore you can not rely on standard Java schedule like the TimerTasks class.

The Android system currently has two main means to schedule tasks:

  • the (outdated) AlarmManager
  • the JobScheduler API.

Modern Android applications should use the JobScheduler API. Apps can schedule jobs while letting the system optimize based on memory, power, and connectivity conditions.

Luckily, using the JobScheduler is pretty straightforward. There’s only three things we need to do:

  1. Create a JobService to handle your job
  2. Add that JobService to the manifest
  3. Schedule your job using a JobInfo object to define your conditions

Creating a JobService

JobService is a specialized Service (i.e. extends Service) with a couple methods to help us handle our job. To create a JobService, start by extending the JobService class and overriding ‘onStartJob’ and ‘onStopJob’. OnStartJob is called by the Android system when it starts your job. However, onStopJob is only called if the job was cancelled before being finished (e.g. we require the device to be charging, and it gets unplugged).

Also, when your job is finished (has been completed or canceled) you’re responsible for calling the ‘jobFinished’ method. The ‘jobFinished’ method tells Android that your job is done and lets it release the wakelock for your app. If you don’t call the ‘jobFinished’ method your app could be responsible for draining your user’s battery!

Here’s an example of a simple Job Service that does 10 seconds of ‘work’ on a new Thread:

public class MyJobService extends JobService {
  private static final String TAG = MyJobService.class.getSimpleName();
  boolean isWorking = false;
  boolean jobCancelled = false;
@Override
public boolean onStartJob(JobParameters jobParameters) {
Log.d(TAG, “Job started!”);
isWorking = true;
startWorkOnNewThread(jobParameters); // Services do NOT run on a separate thread
return isWorking;
}
private void startWorkOnNewThread(final JobParameters jobParameters) {
      new Thread(new Runnable() {
          public void run() {
              doWork(jobParameters);
          }
      }).start();
  }

private void doWork(JobParameters jobParameters) {
      for (int i = 0; i < 1000; i++) {
          if (jobCancelled)
              return;
try {

Thread.sleep(10); } catch (Exception e) { }
      }

Log.d(TAG, “Job finished!”);
      isWorking = false;
      boolean needsReschedule = false;
      jobFinished(jobParameters, needsReschedule);
  }

// Called if the job was cancelled before being finished
  @Override
  public boolean onStopJob(JobParameters jobParameters) {
      Log.d(TAG, “Job cancelled before being completed.”);
      jobCancelled = true;
      boolean needsReschedule = isWorking;
      jobFinished(jobParameters, needsReschedule);
      return needsReschedule;
  }
}

Adding the JobService to the Manifest

Since a JobService IS a Service, you’ll need to declare it in your manifest. The only catch is that since it’s a JobService you also need to include the BIND_JOB_SERVICE permission.

<service
  android:name=”.MyJobService”
  android:permission=”android.permission.BIND_JOB_SERVICE”/>

Scheduling the Job

When it’s time to schedule your job you’ll need to start with a JobInfo object. A JobInfo object contains all your job details and is used by the JobScheduler to schedule your job. To create a JobInfo you’ll need to use a JobInfo.Builder and supply it with a ‘jobId’ (integer of your choosing) and the ComponentName of your JobService. Then you’ll add any conditions you need (e.g. setPeriodic), and build the JobInfo object:

ComponentName componentName = new ComponentName(this, MyJobService.class);
JobInfo jobInfo = new JobInfo.Builder(12, componentName)
      .setRequiresCharging(true)
      .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
      .build();

Once you’ve got your JobInfo, all that’s left is scheduling your job. Just grab a reference to the JobScheduler and call the ‘schedule’ method!

JobScheduler jobScheduler = (JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = jobScheduler.schedule(jobInfo);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
  Log.d(TAG, “Job scheduled!”);
} else {
  Log.d(TAG, “Job not scheduled”);
}

A good way to test that this works would be to run the app on a connected device and then turning the WiFi off to see the job get cancelled. Then turn the WiFi back on, and after a couple minutes you should be able to see your job be restarted. So the next time you’re planning a download in the background, think about giving your users a little more battery life by using the JobScheduler!

  • Share this Article

GET IN TOUCH WITH iLEAF

  • We can initiate development process at the hour of your convenience
  • Discussion on the projects can be held for a stipulated duration
  • We will sign NDA and the talks will be secured
  • We’ll show you around our cherished designs
  • Briefing on technology
  • Guaranteed source code delivery
  • Option to re-start a closed venture

See how we can unwrap your app idea and proceed towards success