在JobService的jobFinished方法中,何时传入true给needsReschedule才合理?
JobService 任务处理与调度详解
Hey there, let's break down how to properly handle JobService task completion, rescheduling, and periodic tasks:
- When your JobService finishes the background work it's supposed to do, make sure to call
jobFinished()to notify the system. This lets the system release the wakelock it's holding—skip this step, and you might end up wasting unnecessary battery power. - The second parameter of
jobFinished()isneedsReschedule, a boolean value:- Pass
trueif you want the system to reschedule the task following its built-in backoff rules. - Pass
falseif the task is fully done and shouldn't run again unless you explicitly schedule it later.
- Pass
- If you need a periodic task, don't rely on setting
needsReschedule=trueto loop the job. Instead, call thesetPeriodic()method on yourJobInfoobject when building it. This is the official, recommended way to set up recurring tasks, as the system will handle the interval-based scheduling automatically. - One important note: If your task gets cancelled (triggering the
onStopJob()method), you don't need to calljobFinished(). The return value ofonStopJob()already controls the rescheduling logic—returntrueto have the system reschedule the task, orfalseto let it end for good.
内容的提问来源于stack exchange,提问作者Florian Walther
相关产品推荐
相关产品推荐

