Android 12 Developer Preview is here! Try it out, and give us your feedback!

Builder

abstract class Builder<B : WorkRequest.Builder<*, *>!, 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
setExpedited(@NonNull policy: OutOfQuotaPolicy)

Marks the WorkRequest as important to the user.

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 TimeUnit: The unit of time for duration
Return
B The current Builder

keepResultsForAtLeast

@RequiresApi(26) @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