本文介绍了任何方式对 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.0PeriodicWorkRequests 支持初始延迟.您可以使用 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:14