android – 任何使PeriodicWorkRequest有初始延迟的方法

前端之家收集整理的这篇文章主要介绍了android – 任何使PeriodicWorkRequest有初始延迟的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对于OneTimeWorkRequest,我们可以将setInitialDelay设置为特定的初始延迟.

但是,PeriodicWorkRequest没有这样的功能.

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

一种不太可靠的方法是使用延迟的OneTimeWorkRequest工作程序来设置PeriodicWorkRequest.但是,这非常麻烦,并且创建了一种可能性,OneTimeWorkRequest可能会失败并且无法安装PeriodicWorkRequest.

解决方法

您可以使用此PeriodicWorkRequest.Builder并提供flexInterval作为第4个参数:
PeriodicWorkRequest build = new PeriodicWorkRequest.Builder(
     SyncJobWorker.class,REPEAT_INTERVAL,// repeatInterval
     TimeUnit.MILLISECONDS,// repeatIntervalTimeUnit
     FLEX_INTERVAL,// flexInterval
     TimeUnit.MILLISECONDS) // flexIntervalTimeUnit
       .build();

文件编号:https://developer.android.com/reference/androidx/work/PeriodicWorkRequest.Builder#periodicworkrequestbuilder_2

Creates a PeriodicWorkRequest to run periodically once within the flex period of every interval period. See diagram below. The flex period begins at intervalMillis – flexMillis to the end of the interval. intervalMillis must be greater than or equal to PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS and flexMillis must be greater than or equal to PeriodicWorkRequest.MIN_PERIODIC_FLEX_MILLIS.

原文链接:https://www.f2er.com/android/314456.html

猜你在找的Android相关文章