WorkRequest.Builder

public static abstract class WorkRequest.Builder
extends Object

java.lang.Object
   ↳ androidx.work.WorkRequest.Builder<B extends androidx.work.WorkRequest.Builder, W extends androidx.work.WorkRequest>


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

Summary

Public methods

final B addTag(String tag)

Adds a tag for the work.

final W build()

Builds a WorkRequest based on this WorkRequest.Builder.

final B keepResultsForAtLeast(long duration, TimeUnit timeUnit)

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

final B keepResultsForAtLeast(Duration duration)

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

final B setBackoffCriteria(BackoffPolicy backoffPolicy, long backoffDelay, TimeUnit timeUnit)

Sets the backoff policy and backoff delay for the work.

final B setBackoffCriteria(BackoffPolicy backoffPolicy, Duration duration)

Sets the backoff policy and backoff delay for the work.

final B setConstraints(Constraints constraints)

Adds constraints to the WorkRequest.

final B setInputData(Data inputData)

Adds input Data to the work.

Inherited methods

Public methods

addTag

public final B addTag (String tag)

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.

Returns
B The current WorkRequest.Builder

build

public final W build ()

Builds a WorkRequest based on this WorkRequest.Builder.

Returns
W A WorkRequest based on this WorkRequest.Builder

keepResultsForAtLeast

public final B keepResultsForAtLeast (long duration, 
                TimeUnit timeUnit)

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

Returns
B The current WorkRequest.Builder

keepResultsForAtLeast

public final B keepResultsForAtLeast (Duration duration)

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 Duration: The minimum duration of time to keep the results of this work

Returns
B The current WorkRequest.Builder

setBackoffCriteria

public final B setBackoffCriteria (BackoffPolicy backoffPolicy, 
                long backoffDelay, 
                TimeUnit timeUnit)

Sets the backoff policy and backoff delay for the work. The default values are BackoffPolicy.EXPONENTIAL and 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 long: Time to wait before retrying the work in timeUnit units

timeUnit TimeUnit: The TimeUnit for backoffDelay

Returns
B The current WorkRequest.Builder

setBackoffCriteria

public final B setBackoffCriteria (BackoffPolicy backoffPolicy, 
                Duration duration)

Sets the backoff policy and backoff delay for the work. The default values are BackoffPolicy.EXPONENTIAL and 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 Duration: Time to wait before retrying the work

Returns
B The current WorkRequest.Builder

setConstraints

public final B setConstraints (Constraints constraints)

Adds constraints to the WorkRequest.

Parameters
constraints Constraints: The constraints for the work

Returns
B The current WorkRequest.Builder

setInputData

public final B setInputData (Data inputData)

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

Returns
B The current WorkRequest.Builder