lightbulb_outline Help shape the future of the Google Play Console, Android Studio, and Firebase. Start survey

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 WorkRequest.

Summary

Public constructors

WorkRequest.Builder(Class<? extends Worker> workerClass)

Public methods

B addTag(String tag)

Add an optional tag for the work.

abstract W build()

Builds this work object.

B keepResultsForAtLeast(long duration, TimeUnit timeUnit)

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

B keepResultsForAtLeast(Duration duration)

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

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

Change backoff policy and delay for the work.

B setConstraints(Constraints constraints)

Add constraints to the OneTimeWorkRequest.

B setInputData(Data inputData)

Add input Data to the work.

Inherited methods

Public constructors

WorkRequest.Builder

public WorkRequest.Builder (Class<? extends Worker> workerClass)

Parameters
workerClass Class

Public methods

addTag

public B addTag (String tag)

Add an optional tag for the work. This is particularly useful for modules or libraries who want to query for or cancel all of their own work.

Parameters
tag String: A tag for identifying the work in queries.

Returns
B The current WorkRequest.Builder

build

public abstract W build ()

Builds this work object.

Returns
W The concrete implementation of the work associated with this builder

keepResultsForAtLeast

public 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 WorkStatus. 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 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 WorkStatus. 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 B setBackoffCriteria (BackoffPolicy backoffPolicy, 
                long backoffDelay, 
                TimeUnit timeUnit)

Change backoff policy and delay for the work. The default is BackoffPolicy.EXPONENTIAL and WorkRequest.DEFAULT_BACKOFF_DELAY_MILLIS. The maximum backoff delay duration is WorkRequest.MAX_BACKOFF_MILLIS.

Parameters
backoffPolicy BackoffPolicy: The BackoffPolicy to use for work

backoffDelay long: Time to wait before restarting Worker in timeUnit units

timeUnit TimeUnit: The TimeUnit for backoffDelay

Returns
B The current WorkRequest.Builder

setConstraints

public B setConstraints (Constraints constraints)

Add constraints to the OneTimeWorkRequest.

Parameters
constraints Constraints: The constraints for the work

Returns
B The current WorkRequest.Builder

setInputData

public B setInputData (Data inputData)

Add input Data to the work.

Parameters
inputData Data: key/value pairs that will be provided to the Worker class

Returns
B The current WorkRequest.Builder