Android JobSchedulers & Usages

Kishan Maurya
MindOrks
Published in
4 min readJun 12, 2019

You’ve seen that you can trigger events based on the real-time clock, or the elapsed time since boot using the AlarmManager class. Most tasks, however, do not require an exact time but should be scheduled based on a combination of system and user requirements.

For example, a news app might like to update the news in the morning but could wait until the device is charging and connected to wifi to update the news, to preserve the user’s data and system resources.

The JobScheduler class is meant for this kind of schedule; it allows you to set the conditions or parameters of running your task. Given these conditions, the JobScheduler calculates the best time to schedule the execution of the job. Some examples of these parameters are the persistence of the job across reboots, the interval that the job should run at, whether or not the device is plugged in, or whether or not the device is idle.

Here is an example when you would use this job scheduler:

1.Tasks that should be done once the device is connected to a power supply

2.Tasks that require network access or a Wi-Fi connection.

3.The task that is not critical or user-facing

4.Tasks that should be running on a regular basis as batch where the timing is not critical

JobScheduler is pretty straightforward. JobScheduler is only available on devices running API 21+.
There are only three things you need to do:

  1. Create a Job Service 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 of methods to help us handle our job.
To create a Job Service, start by extending the JobService class and overriding ‘onStartJob’ and ‘onStopJob’.

OnStartJob is called by the Android system when it starts your job. 
If your task is short & simple,implement the logic directly in onStartJob() and return false when you are finished.
But for complicated task, like network, do in background thread and return true, letting the system know that you have a thread still running and it should hold on to your wakelock for a while longer.
OnStopJob is only called if the job was canceled before being finished (the example we require the device to be charging, and it gets unplugged, or wifi not available). If the job fails for some reason, return true from on the onStopJob to restart the job.

Also, when your job is finished (has been completed or canceled) you’re responsible for calling the job finished 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!

Adding to Manifest

The new JobService must be registered in the Android manifest with the BIND_JOB_SERVICE permission.

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

Creating JobInfo object

All conditions for job scheduler through the JobInfo object.
To build that JobInfo object, you need two things every time
job number: to help you distinguish which job this is &
JobService.

ComponentName componentName = new ComponentName(this, MyJobService.class);JobInfo jobInfo = new JobInfo.Builder(MyJobID, componentName)       .setRequiresCharging(true)
.setPeriodic(50000)
.setRequiresBatteryNotLow(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) .build();
JobScheduler jobScheduler = (JobScheduler)getApplicationContext() .getSystemService(JOB_SCHEDULER_SERVICE);jobScheduler.schedule(jobInfo);

There are many conditions & based on requirement we can set job info objects.

Please use below link:
https://medium.com/google-developers/scheduling-jobs-like-a-pro-with-jobscheduler-286ef8510129
https://www.zoftino.com/android-job-scheduler-example

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

--

--