本文介绍了Android Workmanager PeriodicWorkRequest不是唯一的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于OneTimeWorkRequest,我们可以使用WorkContinuation来确保如果作业已经安排好,我们可以保留或替换它.对于PeriodicWorkRequest没有此类选项,因此,每次创建我的主要活动时,都会创建一个新作业,一段时间后,我会收到此异常.

For OneTimeWorkRequest we can use WorkContinuation to ensure that if the job is already scheduled we can KEEP or REPLACE it.There is no such option for PeriodicWorkRequest, so every time my main activity is created a new job is created and after a while I get this exception.

java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs

因此,我正在尝试以下方法来创建独特的作品"

So I'm trying the following to create a "unique peiodic work"

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));
}

然后在工作结束时再次调用此方法

And then calling this method again at the end of the work

public WorkerResult doWork() {
    try {
        //work to be done
    } catch (Exception e) {
        Log.e(TAG,e.getLocalizedMessage());
        return WorkerResult.FAILURE;
    }
    schedule();
    return WorkerResult.SUCCESS;
}

推荐答案

您看到的 IllegalStateException 是我们在 alpha01 中修复的错误.使用 alpha02 库,您将不会看到该问题.有关更多信息,请在此处中查看.

The IllegalStateException you see was a bug we fixed in alpha01. Use the alpha02 library, and you won't see that problem. For more information, take a look at the release notes here.

这篇关于Android Workmanager PeriodicWorkRequest不是唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:24