Job Scheduler not running within set interval

Papajohn000 picture Papajohn000 · Apr 7, 2015 · Viewed 23k times · Source

I'm trying to use the android Job Scheduler API and all I'm trying to do is have the Job Scheduler run every 5 seconds. However when I run it, the corresponding service is hit every two minutes. I have a log that documents every time the service is hit. I'm not sure why this is happening. Could the Job Scheduler have a minimum interval time. My code is simply...

JobInfo jobInfo = new JobInfo.Builder(1, new ComponentName(this, UpdateDatabaseService.class))
            .setPeriodic(5000)
            .build();

JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(jobInfo);

The problem originally arose when I was trying to run a daily task but it would trigger the service multiple times within that day and wouldn't follow the time guideline.

Let me know what you think.

Answer

jmarkstar picture jmarkstar · May 25, 2017

I was having this problem and after review some blogs and the official documentation, I realised that JobScheduler is having difference behavior on Android N(24 and 25). JobScheduler works with a minimum periodic of 15 mins.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public static void setJobScheduler(Context context){
        Log.v(TAG, "Job Scheduler is starting");
        JobScheduler jobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
        ComponentName serviceName = new ComponentName(context, JobService.class);
        JobInfo jobInfo;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
                    .setPeriodic(900000)
                    .build();
        }else{
            jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
                    .setPeriodic(Constants.TIME_INTERVAL)
                    .build();
        }
        jobScheduler.schedule(jobInfo);
    }