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

帅比萌擦擦* 提交于 2019-12-02 08:46:26

问题


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;
  }

}

回答1:


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);



回答2:


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)



来源:https://stackoverflow.com/questions/45736914/job-scheduler-not-recurring-in-periodic-in-android-7-0-nougat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!