如何用相同的id安排更多的工作
How to schedule more job with the same id
我想使用 android JobScheduler
来安排一项或多项工作。问题是工作编号。我怎样才能 select 每次都做不同的工作?如果我安排具有相同 ID 的新工作,旧工作将被删除。我需要 enqueuWork
之类的东西,但你需要 Api 级别 26,我的最低 api 级别是 21。我该怎么做?每个工作都与同一个工作相关,例如:发送邮件。它只是一个工作,但它可以有多个实例。
不确定 JobScheduler 是否有该选项,但如果您能够为另一个组件更改它,我会推荐您:FireBase JobDispatcher
它有这个选项:.setReplaceCurrent(false)
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
Job myJob = dispatcher.newJobBuilder()
// the JobService that will be called
.setService(MyJobService.class)
// uniquely identifies the job
.setTag("my-unique-tag")
// one-off job
.setRecurring(false)
// don't persist past a device reboot
.setLifetime(Lifetime.UNTIL_NEXT_BOOT)
// start between 0 and 60 seconds from now
.setTrigger(Trigger.executionWindow(0, 60))
// don't overwrite an existing job with the same tag
.setReplaceCurrent(false)
// retry with exponential backoff
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
// constraints that need to be satisfied for the job to run
.setConstraints(
// only run on an unmetered network
Constraint.ON_UNMETERED_NETWORK,
// only run when the device is charging
Constraint.DEVICE_CHARGING
)
.setExtras(myExtrasBundle)
.build();
dispatcher.mustSchedule(myJob);
我不得不使用它并且效果很好
您可以使用 JobScheduler
的 getAllPendingJobs()
方法找出可用的 jobID
。它将 return 所有计划的待处理作业,而它们是 pending/running。 JobSchedulerService
会从此列表中删除具有 finished/completed 的作业。
Obs.: Android doc describes:
"Retrieve all jobs for this package that are pending in the JobScheduler
."
我已经测试了这个方法(在 Android
M 和 N 上),它也 return 是 运行 的工作。作业完成后已删除。在 Android 源代码、getAllPendingJobs()
uses mJobs
(stores all the scheduled jobs) to return a job list. When the Job is Stopped 中,它已从 mJobs
中删除,并且在 getAllPendingJobs ()
中未被 return 编辑].
我想使用 android JobScheduler
来安排一项或多项工作。问题是工作编号。我怎样才能 select 每次都做不同的工作?如果我安排具有相同 ID 的新工作,旧工作将被删除。我需要 enqueuWork
之类的东西,但你需要 Api 级别 26,我的最低 api 级别是 21。我该怎么做?每个工作都与同一个工作相关,例如:发送邮件。它只是一个工作,但它可以有多个实例。
不确定 JobScheduler 是否有该选项,但如果您能够为另一个组件更改它,我会推荐您:FireBase JobDispatcher
它有这个选项:.setReplaceCurrent(false)
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
Job myJob = dispatcher.newJobBuilder()
// the JobService that will be called
.setService(MyJobService.class)
// uniquely identifies the job
.setTag("my-unique-tag")
// one-off job
.setRecurring(false)
// don't persist past a device reboot
.setLifetime(Lifetime.UNTIL_NEXT_BOOT)
// start between 0 and 60 seconds from now
.setTrigger(Trigger.executionWindow(0, 60))
// don't overwrite an existing job with the same tag
.setReplaceCurrent(false)
// retry with exponential backoff
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
// constraints that need to be satisfied for the job to run
.setConstraints(
// only run on an unmetered network
Constraint.ON_UNMETERED_NETWORK,
// only run when the device is charging
Constraint.DEVICE_CHARGING
)
.setExtras(myExtrasBundle)
.build();
dispatcher.mustSchedule(myJob);
我不得不使用它并且效果很好
您可以使用 JobScheduler
的 getAllPendingJobs()
方法找出可用的 jobID
。它将 return 所有计划的待处理作业,而它们是 pending/running。 JobSchedulerService
会从此列表中删除具有 finished/completed 的作业。
Obs.: Android doc describes: "Retrieve all jobs for this package that are pending in the
JobScheduler
."
我已经测试了这个方法(在 Android
M 和 N 上),它也 return 是 运行 的工作。作业完成后已删除。在 Android 源代码、getAllPendingJobs()
uses mJobs
(stores all the scheduled jobs) to return a job list. When the Job is Stopped 中,它已从 mJobs
中删除,并且在 getAllPendingJobs ()
中未被 return 编辑].