对于OneTimeWorkRequest,我们可以使用WorkContinuation来确保如果作业已经被安排,我们可以选择保留或替换它。 但是对于PeriodicWorkRequest,没有这样的选项,因此每次创建主活动时都会创建一个新的作业,过一段时间后就会出现此异常。
java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
所以我正在尝试以下方法创建“唯一周期性工作”
public void schedule(){
Constraints constraints = new Constraints.Builder().setRequiresBatteryNotLow(true).build();
OneTimeWorkRequest zombieSpawnWorker = new OneTimeWorkRequest.Builder(ZombieSpawnWorker
.class).setInitialDelay(15, TimeUnit.MINUTES).setConstraints(constraints).addTag(ZombieSpawnWorker.TAG).build();
this.setUuid(zombieSpawnWorker.getId());
WorkManager.getInstance().beginUniqueWork(TAG,
ExistingWorkPolicy.KEEP,
OneTimeWorkRequest.from(ZombieSpawnWorker.class));
}
然后在工作结束时再次调用此方法。
public WorkerResult doWork() {
try {
//work to be done
} catch (Exception e) {
Log.e(TAG,e.getLocalizedMessage());
return WorkerResult.FAILURE;
}
schedule();
return WorkerResult.SUCCESS;
}
WorkManager
不适合安排超过100个提醒? - Cheok Yan Cheng