以任何方式对PeriodicWorkRequest进行初始延迟

以任何方式对PeriodicWorkRequest进行初始延迟

本文介绍了以任何方式对PeriodicWorkRequest进行初始延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 OneTimeWorkRequest ,我们可以使用 setInitialDelay 来指定初始延迟.

For OneTimeWorkRequest, we can have setInitialDelay to specific the initial delay.

但是,没有针对 PeriodicWorkRequest 的工具.

However, there isn't such facility for PeriodicWorkRequest.

有没有可靠的方法来实现这一目标?

一种不那么可靠的方法是让一个延迟的 OneTimeWorkRequest 工作者来设置 PeriodicWorkRequest .但是,这非常麻烦,并且可能导致 OneTimeWorkRequest 失败并且无法安装 PeriodicWorkRequest .

One of the less reliable way, is to have a delayed OneTimeWorkRequest worker, to setup PeriodicWorkRequest. However, that's quite cumbersome, and create a possibility, where OneTimeWorkRequest may fail and not able to install PeriodicWorkRequest.

推荐答案

由于 androidx.work:work-*:2.1.0 PeriodicWorkRequests 支持初始延迟.您可以在 PeriodicWorkRequest.Builder 上使用 setInitialDelay 方法来设置初始延迟.

Since androidx.work:work-*:2.1.0, PeriodicWorkRequests support initial delays. You can use the setInitialDelay method on PeriodicWorkRequest.Builder to set an initial delay.

有关官方文档,请参见链接.

See link for official documentation.

这篇关于以任何方式对PeriodicWorkRequest进行初始延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:13