本文介绍了使用WorkManager的定期日常工作请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正确使用Android Jetpack中的新版WorkManager安排每天一次的定期工作,该工作每天应做一些正确的动作?

How to properly use the new WorkManager from Android Jetpack to schedule a one per day periodic work that should do some action on a daily basis and exactly one time?

想法是使用WorkManager检查具有给定标签的作品是否已经存在,否则开始新的定期工作.

The idea was to check if the work with a given tag already exists using WorkManager and to start a new periodic work otherwise.

我尝试使用下一种方法:

I've tried to do it using next approach:

public static final String CALL_INFO_WORKER = "Call worker";

...

WorkManager workManager = WorkManager.getInstance();
List<WorkStatus> value = workManager.getStatusesByTag(CALL_INFO_WORKER).getValue();
if (value == null) {
    WorkRequest callDataRequest = new PeriodicWorkRequest.Builder(CallInfoWorker.class,
                24, TimeUnit.HOURS, 3, TimeUnit.HOURS)
                .addTag(CALL_INFO_WORKER)
                .build();
    workManager.enqueue(callDataRequest);
}

但是value始终为null,即使我在WorkerdoWork()方法中放置一个断点(因此它肯定正在进行中)并从另一个线程检查工作状态.

But the value is always null, even if I put a breakpoint inside the Worker's doWork() method (so it is definitely in progress) and check the work status from another thread.

推荐答案

您现在可以使用enqueueUniquePeriodicWork方法.它是在WorkManager的1.0.0-alpha03版本中添加的.

You can now use enqueueUniquePeriodicWork method. It was added in 1.0.0-alpha03 release of the WorkManager.

这篇关于使用WorkManager的定期日常工作请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 10:24