简体   繁体   中英

Android JobScheduler setRequiresDeviceIdle

How exactly does the method "setRequireDeviceIdle" from JobInfo.Builder works? From what I understanded in Android documentation ( https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#setRequiresDeviceIdle(boolean) ) it should guarantee that your jobs only launches if there was no interaction with the device.

My experience it's different:

private void scheduleRestartJob(){
    ComponentName mServiceComponent = new ComponentName(this, RelaunchAppJobService.class);

    JobInfo.Builder builder = new JobInfo.Builder(1002, mServiceComponent);
    builder.setRequiresDeviceIdle(true);
    builder.setPersisted(true);
    builder.setOverrideDeadline(10000);
    mJobScheduler.schedule(builder.build());
}

If I quit my app, and start to use another one, it doesn't matter if I'm active or not, since the job will always run after the 10 seconds.

If I quit my app, and start to use another one, it doesn't matter if I'm active or not, since the job will always run after the 10 seconds.

That is because of this line:

builder.setOverrideDeadline(10000);

Quoting the documentation :

The job will be run by this deadline even if other requirements are not met

(emphasis added)

So, your job will run when the device goes idle or 10 seconds from now, whichever comes first. In all likelihood, the 10 seconds will elapse first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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