问题
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