Stay organized with collections Save and categorize content based on your preferences.

PeriodicWorkRequest.Builder

public final class PeriodicWorkRequest.Builder extends WorkRequest.Builder


Builder for PeriodicWorkRequests.

Summary

Public constructors

@RequiresApi(value = 26)
Builder(
    @NonNull Class<@NonNull ListenableWorker> workerClass,
    @NonNull Duration repeatInterval
)

Creates a PeriodicWorkRequest to run periodically once every interval period.

@RequiresApi(value = 26)
Builder(
    @NonNull Class<ListenableWorker> workerClass,
    @NonNull Duration repeatInterval,
    @NonNull Duration flexInterval
)

Creates a PeriodicWorkRequest to run periodically once within the flex period of every interval period.

Builder(
    @NonNull Class<ListenableWorker> workerClass,
    long repeatInterval,
    @NonNull TimeUnit repeatIntervalTimeUnit
)

Creates a PeriodicWorkRequest to run periodically once every interval period.

Builder(
    @NonNull Class<ListenableWorker> workerClass,
    long repeatInterval,
    @NonNull TimeUnit repeatIntervalTimeUnit,
    long flexInterval,
    @NonNull TimeUnit flexIntervalTimeUnit
)

Creates a PeriodicWorkRequest to run periodically once within the flex period of every interval period.

Inherited methods

From androidx.work.WorkRequest.Builder
final @NonNull PeriodicWorkRequest.Builder

Adds a tag for the work.

final @NonNull PeriodicWorkRequest

Builds a WorkRequest based on this Builder.

final @NonNull PeriodicWorkRequest.Builder

Specifies that the results of this work should be kept for at least the specified amount of time.

final @NonNull PeriodicWorkRequest.Builder
keepResultsForAtLeast(long duration, @NonNull TimeUnit timeUnit)

Specifies that the results of this work should be kept for at least the specified amount of time.

final @NonNull PeriodicWorkRequest.Builder
@RequiresApi(value = 26)
setBackoffCriteria(
    @NonNull BackoffPolicy backoffPolicy,
    @NonNull Duration duration
)

Sets the backoff policy and backoff delay for the work.

final @NonNull PeriodicWorkRequest.Builder
setBackoffCriteria(
    @NonNull BackoffPolicy backoffPolicy,
    long backoffDelay,
    @NonNull TimeUnit timeUnit
)

Sets the backoff policy and backoff delay for the work.

final @NonNull PeriodicWorkRequest.Builder

Adds constraints to the WorkRequest.

@NonNull PeriodicWorkRequest.Builder

Marks the WorkRequest as important to the user.

final @NonNull PeriodicWorkRequest.Builder

The id of the request.

@NonNull PeriodicWorkRequest.Builder
@RequiresApi(value = 26)
setInitialDelay(@NonNull Duration duration)

Sets an initial delay for the WorkRequest.

@NonNull PeriodicWorkRequest.Builder
setInitialDelay(long duration, @NonNull TimeUnit timeUnit)

Sets an initial delay for the WorkRequest.

final @NonNull PeriodicWorkRequest.Builder

Adds input Data to the work.

Public constructors

Builder

@RequiresApi(value = 26)
public Builder(
    @NonNull Class<@NonNull ListenableWorker> workerClass,
    @NonNull Duration repeatInterval
)

Creates a PeriodicWorkRequest to run periodically once every interval period. The PeriodicWorkRequest is guaranteed to run exactly one time during this interval (subject to OS battery optimizations, such as doze mode). The repeat interval must be greater than or equal to PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS. It may run immediately, at the end of the period, or any time in between so long as the other conditions are satisfied at the time. The run time of the PeriodicWorkRequest can be restricted to a flex period within an interval (see #Builder(Class, Duration, Duration)).

Parameters
@NonNull Class<@NonNull ListenableWorker> workerClass

The ListenableWorker class to run for this work

@NonNull Duration repeatInterval

The repeat interval

Builder

@RequiresApi(value = 26)
public Builder(
    @NonNull Class<ListenableWorker> workerClass,
    @NonNull Duration repeatInterval,
    @NonNull Duration flexInterval
)

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

[_____before flex_____|_____flex_____][_____before flex_____|_____flex_____]...
[___cannot run work___|_can run work_][___cannot run work___|_can run work_]...
\____________________________________/\____________________________________/...
interval 1 interval 2 ...(repeat)
Parameters
@NonNull Class<ListenableWorker> workerClass

The ListenableWorker class to run for this work

@NonNull Duration repeatInterval

The repeat interval

@NonNull Duration flexInterval

The duration in for which this work repeats from the end of the repeatInterval

Builder

public Builder(
    @NonNull Class<ListenableWorker> workerClass,
    long repeatInterval,
    @NonNull TimeUnit repeatIntervalTimeUnit
)

Creates a PeriodicWorkRequest to run periodically once every interval period. The PeriodicWorkRequest is guaranteed to run exactly one time during this interval (subject to OS battery optimizations, such as doze mode). The repeat interval must be greater than or equal to PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS. It may run immediately, at the end of the period, or any time in between so long as the other conditions are satisfied at the time. The run time of the PeriodicWorkRequest can be restricted to a flex period within an interval (see #Builder(Class, long, TimeUnit, long, TimeUnit)).

Parameters
@NonNull Class<ListenableWorker> workerClass

The ListenableWorker class to run for this work

long repeatInterval

The repeat interval in repeatIntervalTimeUnit units

@NonNull TimeUnit repeatIntervalTimeUnit

The TimeUnit for repeatInterval

Builder

public Builder(
    @NonNull Class<ListenableWorker> workerClass,
    long repeatInterval,
    @NonNull TimeUnit repeatIntervalTimeUnit,
    long flexInterval,
    @NonNull TimeUnit flexIntervalTimeUnit
)

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

[_____before flex_____|_____flex_____][_____before flex_____|_____flex_____]...
[___cannot run work___|_can run work_][___cannot run work___|_can run work_]...
\____________________________________/\____________________________________/...
interval 1 interval 2 ...(repeat)
Parameters
@NonNull Class<ListenableWorker> workerClass

The ListenableWorker class to run for this work

long repeatInterval

The repeat interval in repeatIntervalTimeUnit units

@NonNull TimeUnit repeatIntervalTimeUnit

The TimeUnit for repeatInterval

long flexInterval

The duration in flexIntervalTimeUnit units for which this work repeats from the end of the repeatInterval

@NonNull TimeUnit flexIntervalTimeUnit

The TimeUnit for flexInterval