Builder

abstract class Builder<B : WorkRequest.Builder<WorkRequest.Builder<*, *>, WorkRequest!>!, W : WorkRequest!>
kotlin.Any
   ↳ androidx.work.WorkRequest.Builder

A builder for WorkRequests. There are two concrete implementations of this class: OneTimeWorkRequest.Builder and PeriodicWorkRequest.Builder.

Summary

Public methods

B
addTag(@NonNull tag: String)

Adds a tag for the work.

W

Builds a WorkRequest based on this Builder.

B
keepResultsForAtLeast(duration: Long, @NonNull timeUnit: TimeUnit)

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

B
keepResultsForAtLeast(@NonNull duration: Duration)

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

B
setBackoffCriteria(@NonNull backoffPolicy: BackoffPolicy, backoffDelay: Long, @NonNull timeUnit: TimeUnit)

Sets the backoff policy and backoff delay for the work.

B
setBackoffCriteria(@NonNull backoffPolicy: BackoffPolicy, @NonNull duration: Duration)

Sets the backoff policy and backoff delay for the work.

B
setConstraints(@NonNull constraints: Constraints)

Adds constraints to the WorkRequest.

open B
setInitialDelay(duration: Long, @NonNull timeUnit: TimeUnit)

Sets an initial delay for the WorkRequest.

open B
setInitialDelay(@NonNull duration: Duration)

Sets an initial delay for the WorkRequest.

B
setInputData(@NonNull inputData: Data)

Adds input Data to the work.

Public methods

addTag

@NonNull fun addTag(@NonNull tag: String): B

Adds a tag for the work. You can query and cancel work by tags. Tags are particularly useful for modules or libraries to find and operate on their own work.

Parameters
tag String: A tag for identifying the work in queries.
Return
B: The current Builder

build

@NonNull fun build(): W

Builds a WorkRequest based on this Builder.

Return
W: A WorkRequest based on this Builder

keepResultsForAtLeast

@NonNull fun keepResultsForAtLeast(duration: Long, @NonNull timeUnit: TimeUnit): B

Specifies that the results of this work should be kept for at least the specified amount of time. After this time has elapsed, the results may be pruned at the discretion of WorkManager when there are no pending dependent jobs.

When the results of a work are pruned, it becomes impossible to query for its WorkInfo.

Specifying a long duration here may adversely affect performance in terms of app storage and database query time.

Parameters
duration Long: The minimum duration of time (in timeUnit units) to keep the results of this work
timeUnit Long: The unit of time for duration
Return
B: The current Builder

keepResultsForAtLeast

@NonNull fun keepResultsForAtLeast(@NonNull duration: Duration): B

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

may

be pruned at the discretion of WorkManager when this WorkRequest has reached a finished state (see WorkInfo.State#isFinished()) and there are no pending dependent jobs.

When the results of a work are pruned, it becomes impossible to query for its WorkInfo.

Specifying a long duration here may adversely affect performance in terms of app storage and database query time.

Parameters
duration Duration: The minimum duration of time to keep the results of this work
Return
B: The current Builder

setBackoffCriteria

@NonNull fun setBackoffCriteria(@NonNull backoffPolicy: BackoffPolicy, backoffDelay: Long, @NonNull timeUnit: TimeUnit): B

Sets the backoff policy and backoff delay for the work. The default values are BackoffPolicy#EXPONENTIAL and {@value WorkRequest#DEFAULT_BACKOFF_DELAY_MILLIS}, respectively. backoffDelay will be clamped between WorkRequest#MIN_BACKOFF_MILLIS and WorkRequest#MAX_BACKOFF_MILLIS.

Parameters
backoffPolicy BackoffPolicy: The BackoffPolicy to use when increasing backoff time
backoffDelay BackoffPolicy: Time to wait before retrying the work in timeUnit units
timeUnit BackoffPolicy: The TimeUnit for backoffDelay
Return
B: The current Builder

setBackoffCriteria

@NonNull fun setBackoffCriteria(@NonNull backoffPolicy: BackoffPolicy, @NonNull duration: Duration): B

Sets the backoff policy and backoff delay for the work. The default values are BackoffPolicy#EXPONENTIAL and {@value WorkRequest#DEFAULT_BACKOFF_DELAY_MILLIS}, respectively. duration will be clamped between WorkRequest#MIN_BACKOFF_MILLIS and WorkRequest#MAX_BACKOFF_MILLIS.

Parameters
backoffPolicy BackoffPolicy: The BackoffPolicy to use when increasing backoff time
duration BackoffPolicy: Time to wait before retrying the work
Return
B: The current Builder

setConstraints

@NonNull fun setConstraints(@NonNull constraints: Constraints): B

Adds constraints to the WorkRequest.

Parameters
constraints Constraints: The constraints for the work
Return
B: The current Builder

setInitialDelay

@NonNull open fun setInitialDelay(duration: Long, @NonNull timeUnit: TimeUnit): B

Sets an initial delay for the WorkRequest.

Parameters
duration Long: The length of the delay in timeUnit units
timeUnit Long: The units of time for duration
Return
B: The current Builder

setInitialDelay

@NonNull open fun setInitialDelay(@NonNull duration: Duration): B

Sets an initial delay for the WorkRequest.

Parameters
duration Duration: The length of the delay
Return
B: The current Builder

setInputData

@NonNull fun setInputData(@NonNull inputData: Data): B

Adds input Data to the work. If a worker has prerequisites in its chain, this Data will be merged with the outputs of the prerequisites using an InputMerger.

Parameters
inputData Data: key/value pairs that will be provided to the worker
Return
B: The current Builder