JobScheduler class is used to schedule Jobs in Android.
JobScheduler was added recently in API 21, so below that it will not work.

Here is a simple example…

Schedule a job that runs every 5 seconds.

We need to create the JobSchedulerService class at first.


import android.util.Log;

public class JobSchedulerService extends JobService {
    private static final String TAG = "JobSchedulerService";
    public boolean onStartJob(JobParameters params) {
        Log.i(TAG, "onStartJob:");
        return false;

    public boolean onStopJob(JobParameters params) {
        Log.i(TAG, "onStopJob:");
        return false;



Just like a service, we need to add this service in the AndroidManifest.

<service android:name=".JobSchedulerService"

Create Scheduler Objects
Now we will create the JobScheduler Object and schedule the Job.


import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    JobScheduler mJobScheduler;

    protected void onCreate(Bundle savedInstanceState) {

        Log.i(TAG, "onCreate: ");

        mJobScheduler = (JobScheduler)
        JobInfo.Builder builder = new JobInfo.Builder(1,
                new ComponentName(getPackageName(),

        if (mJobScheduler.schedule( <= 0) {
            Log.e(TAG, "onCreate: Some error while scheduling the job");


More Parameters

  • setMinimumLatency(long minLatencyMillis): This makes your job not launch until the stated number of milliseconds have passed. This is incompatible with setPeriodic(long time) and will cause an exception to be thrown if they are both used.
  • setOverrideDeadline(long maxExecutionDelayMillis): This will set a deadline for your job. Even if other requirements are not met, your task will start approximately when the stated time has passed. Like setMinimumLatency(long time), this function is mutually exclusive with setPeriodic(long time) and will cause an exception to be thrown if they are both used.
  • setPersisted(boolean isPersisted): This function tells the system whether your task should continue to exist after the device has been rebooted.
  • setRequiredNetworkType(int networkType): This function will tell your job that it can only start if the device is on a specific kind of network. The default is JobInfo.NETWORK_TYPE_NONE, meaning that the task can run whether there is network connectivity or not. The other two available types are JobInfo.NETWORK_TYPE_ANY, which requires some type of network connection available for the job to run, and JobInfo.NETWORK_TYPE_UNMETERED, which requires that the device be on a non-cellular network.
  • setRequiresCharging(boolean requiresCharging): Using this function will tell your application that the job should not start until the device has started charging.
  • setRequiresDeviceIdle(boolean requiresDeviceIdle): This tells your job to not start unless the user is not using their device and they have not used it for some time.

All Done. Now run the app and check the logs.


Leave a Reply

Your email address will not be published. Required fields are marked *




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.