Any way to have initial delay for PeriodicWorkRequest

后端 未结 2 1160
粉色の甜心
粉色の甜心 2021-02-14 12:40

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

However, there isn\'t such facility for PeriodicWorkRe

相关标签:
2条回答
  • 2021-02-14 13:08

    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.

    0 讨论(0)
  • 2021-02-14 13:22

    Since the system runs your work you can't control the exact time it will run..

    Your best option for creating a work execution delay is to use this PeriodicWorkRequest.Builder and supply a flexInterval as the 4th parameter:

    PeriodicWorkRequest build = new PeriodicWorkRequest.Builder(
         SyncJobWorker.class, 
         REPEAT_INTERVAL, // repeatInterval
         TimeUnit.MILLISECONDS, // repeatIntervalTimeUnit
         FLEX_INTERVAL, // flexInterval
         TimeUnit.MILLISECONDS) // flexIntervalTimeUnit
           .build();
    

    Docs ref: 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.

    0 讨论(0)
提交回复
热议问题