JobScheduler in Android

We can use the AlarmManager class to trigger events based on the real-time clock, or based on elapsed time since boot.

But every tasks, do not require the same time, instead it should be scheduled based on a system and user requirements.

For example, to save the user’s data and system resources, a news app could wait until the device is charging and conneconted to Wi-Fi to update the news.

The JobScheduler class allows you to run your task based on the conditions, or parameters.

JobScheduler calculates the best time to schedule the execution of the job.

For example, We can set job parameters which include the persistence of the job across reboots, whether the device is plugged in or not, or whether the device is idle or active.

JobScheduler is only available on devices running API 21 and higher, and is currently not available in the support library.

For backward compatibility, we use WorkManager. The WorkManager API lets you schedule background tasks that need guaranteed completion, whether or not the app process is around.

For devices running API 14 and higher, including devices without Google Play services, WorkManager provides capabilities that are like those provided by JobScheduler.

How to implement a JobService

We will create a service that will automatically be executed based on the conditions we set.

We only need to implement onStartJob() and onStopJob() callback methods.

public class AndroidJobService extends JobService {

    @Override
    public boolean onStartJob(JobParameters params) {
        // write down task here
        // For Example Fetch Data From Server and Store in Local (Offline) Database
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        //if the job fails, you want the job to be rescheduled instead of dropped
        return true;
    }
}

About onStartJob()

It will be Called when the system determines that your task should be run.
In this method, you implement the job to be done.

It will return a boolean value which indicates whether you want the job to continue on a separate thread.

If you return true then you need to call jobFinished() explicitly to indicate that the job is completed successfully

If you return false, that means the job is completed by the end of onStartJob(), and the system calls jobFinished() implicitly.

About the onStopJob()

The conditions you mentioned in the JobInfo not met, the job must be stopped, and the system calls onStopJob()

This method also returns a boolean that determines what to do if the job is not finished.

If you return value is true, the job is rescheduled; otherwise, the job is dropped.

In your AndroidManfest.xml file

<service   android:name=".AndroidJobService"   android:permission="android.permission.BIND_JOB_SERVICE"/>

Schedule Job

To Schedule the job we need to create JobScheduler instance first as given below

JobScheduler jobScheduler = (JobScheduler)getApplicationContext().getSystemService(JOB_SCHEDULER_SERVICE);

Then we can create ComponentName instance which will wrap our AndroidJobService class

ComponentName componentName = new ComponentName(this,AndroidJobService.class);

Then we need to create JobInfo instance with required conditions and use schedule method as given

JobInfo jobInfo = new JobInfo.Builder(1, componentName)
.setPeriodic(86400000)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true).build();
jobScheduler.schedule(jobInfo);

Related Posts

Leave a Reply