简体   繁体   English

Job Scheduler在Android 7.0(Nougat)中不定期出现

[英]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. 我的要求是无论使用Android 7.0及更高版本的Job Scheduler,每10分钟执行一次作业。 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;
  }

}

in Android N (Nougat) minimum period interval is 15 minutes . 在Android N(牛轧糖)中,最小时间间隔为15分钟。 Set your interval to 15 minutes then the code will work. 将您的时间间隔设置为15分钟,代码即可正常工作。

And also set 并同时设定

jobFinished(parameters, false);

The JobScheduler is optimized by the Android OS, therefor your job never will be executed at the exact interval you specified. JobScheduler已通过Android操作系统进行了优化,因此您的作业将永远不会以您指定的确切时间间隔执行。

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) https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#setPeriodic(long)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM