Niraj Zurmure
Niraj Zurmure

Reputation: 11

Job Scheduler Not recurring in periodic in Android 7.0 (Nougat)

Job is not firing on given time...it delays ...delays...delay time increases. my requirement is to perform job no matter what in every 10 mins using Job Scheduler in Android 7.0 and above. here my code snippet

private static long Scheduler_Interval = 5 * DateUtils.MINUTE_IN_MILLIS;

JobScheduler mJobScheduler mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);

            JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(getPackageName(), JobSchedulerService.class.getName()));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                builder.setPeriodic(Scheduler_Interval, 1 * DateUtils.MINUTE_IN_MILLIS);
            }
            builder.setRequiresDeviceIdle(false);

            if (mJobScheduler.schedule(builder.build()) <= 0) {
                ShowToast("Some error while scheduling the job");
            }


public class JobSchedulerService extends JobService {
 @Override
  public boolean onStartJob(JobParameters jobParameters) {
      writeToTestLogFile(GetSavedDateFromLocationProvider()+ "|onStartJob");
      return false;
  }

  @Override
  public boolean onStopJob(JobParameters jobParameters) {
      writeToTestLogFile(GetSavedDateFromLocationProvider()+ "|onStopJob");
     return false;
  }

}

Upvotes: 1

Views: 3740

Answers (2)

MarGin
MarGin

Reputation: 2518

in Android N (Nougat) minimum period interval is 15 minutes . Set your interval to 15 minutes then the code will work.

And also set

jobFinished(parameters, false);

Upvotes: 2

Piwo
Piwo

Reputation: 1509

The JobScheduler is optimized by the Android OS, therefor your job never will be executed at the exact interval you specified.

Specify that this job should recur with the provided interval, not more than once per period. You have no control over when within this interval this job will be executed, only the guarantee that it will be executed at most once within this interval.

https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#setPeriodic(long)

Upvotes: 0

Related Questions