Android - Using JobService & JobScheduler For Running Repeated Background Jobs

Does your Android application needs to run a background job repeatedly every certain time? There are some ways to do it. One of the simplest method is by creating a JobService which is scheduled by a JobScheduler. To use JobScheduler, the Android version must be at least version 5.0 Lollipop or above. This tutorial shows you how to schedule a background job using JobScheduler

First, we create a class that extends JobService. It means you have to override two required methods:

  • public boolean onStartJob(JobParameters params)
  • public boolean onStopJob(JobParameters params)

Inside onStartJob, you have to implement what the job should do every time it's executed.

onStopJob is called if the system has determined that you must stop execution of your job even before you've had a chance to call jobFinished(JobParameters, boolean). If you don't need to handle that condition, just return true.

  public class MyJobService extends JobService {
      @Override
      public boolean onStartJob(JobParameters params) {
          // Write your code here
          
          return true;
      }

      @Override
      public boolean onStopJob(JobParameters params) {
          return true;
      }

  }

The service must be registered in AndroidManifest.xml. Inside <application> tag, add the following code.

  <service
      android:name=".services.TestJobService"
      android:label="Word service"
      android:permission="android.permission.BIND_JOB_SERVICE" >

  </service>

Next, we create a util which responsibles for scheduling the job using JobScheduler. The code below will schedule to run MyJobService every 30 ~ 60 seconds

  public class Util {

      public static void scheduleJob(Context context) {
          ComponentName serviceComponent = new ComponentName(context, MyJobService.class);
          JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
          builder.setMinimumLatency(30 * 1000); // Wait at least 30s
          builder.setOverrideDeadline(60 * 1000); // Maximum delay 60s

          JobScheduler jobScheduler = (JobScheduler)context.getSystemService(context.JOB_SCHEDULER_SERVICE);
          jobScheduler.schedule(builder.build());
      }

  }

Every time scheduleJob is called, it will execute MyJobService and the code inside onStartJob will be executed. After the job finished, it should be scheduled again. So, we need to call Util.scheduleJob(context); at the bottom of onStartJob.

The question is who will trigger the scheduleJob method. We need a service for triggering it. Create a class that extends BroadcastReceiver and overrides the method you have to override: public void onReceive(Context context, Intent intent). Inside the method, call the scheduleJob.

  public class MyStartServiceReceiver extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {
          Util.scheduleJob(context);
      }
  }

To trigger the receiver, add it in AndroidManifest.xml inside <application> tag. We need to define action that will trigger the receiver to start. In this example, there are two actions that can trigger the receiver: BOOT_COMPLETED (after a "cold" boot) and QUICKBOOT_POWERON (after restart)

  <receiver android:name=".services.MyStartServiceReceiver" >
      <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED" />
          <action android:name="android.intent.action.QUICKBOOT_POWERON" />
      </intent-filter>
  </receiver>

If you want to use other action for triggering the BroadcastReceiver, click here for full list of supported actions. Read the Broadcast Action description to make sure you choose the right action.

Then, do action according to what you set in intent-filter, so that the BroadcastReceiver will be called. If you use the same actions set in this tutorial, just restart your phone.