public abstract class

WorkRequest.Builder<B extends WorkRequest.Builder, W extends WorkRequest>

extends java.lang.Object

 java.lang.Object

↳androidx.work.WorkRequest.Builder<B, W>

Subclasses:

PeriodicWorkRequest.Builder, OneTimeWorkRequest.Builder

Overview

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

Summary

Methods
public final WorkRequest.Builder<B, W>addTag(java.lang.String tag)

Adds a tag for the work.

public final WorkRequestbuild()

Builds a WorkRequest based on this WorkRequest.Builder.

public final WorkRequest.Builder<B, W>keepResultsForAtLeast(java.time.Duration duration)

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

public final WorkRequest.Builder<B, W>keepResultsForAtLeast(long duration, java.util.concurrent.TimeUnit timeUnit)

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

public final WorkRequest.Builder<B, W>setBackoffCriteria(BackoffPolicy backoffPolicy, java.time.Duration duration)

Sets the backoff policy and backoff delay for the work.

public final WorkRequest.Builder<B, W>setBackoffCriteria(BackoffPolicy backoffPolicy, long backoffDelay, java.util.concurrent.TimeUnit timeUnit)

Sets the backoff policy and backoff delay for the work.

public final WorkRequest.Builder<B, W>setConstraints(Constraints constraints)

Adds constraints to the WorkRequest.

public WorkRequest.Builder<B, W>setExpedited(OutOfQuotaPolicy policy)

Marks the WorkRequest as important to the user.

public WorkRequest.Builder<B, W>setInitialDelay(java.time.Duration duration)

Sets an initial delay for the WorkRequest.

public WorkRequest.Builder<B, W>setInitialDelay(long duration, java.util.concurrent.TimeUnit timeUnit)

Sets an initial delay for the WorkRequest.

public final WorkRequest.Builder<B, W>setInitialRunAttemptCount(int runAttemptCount)

Sets the initial run attempt count for this work.

public final WorkRequest.Builder<B, W>setInitialState(WorkInfo.State state)

Sets the initial state for this work.

public final WorkRequest.Builder<B, W>setInputData(Data inputData)

Adds input Data to the work.

public final WorkRequest.Builder<B, W>setLastEnqueueTime(long periodStartTime, java.util.concurrent.TimeUnit timeUnit)

Sets the period start time for this work.

public final WorkRequest.Builder<B, W>setScheduleRequestedAt(long scheduleRequestedAt, java.util.concurrent.TimeUnit timeUnit)

Sets when the scheduler actually schedules the worker.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public final WorkRequest.Builder<B, W> setBackoffCriteria(BackoffPolicy backoffPolicy, long backoffDelay, java.util.concurrent.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: The BackoffPolicy to use when increasing backoff time
backoffDelay: Time to wait before retrying the work in timeUnit units
timeUnit: The java.util.concurrent.TimeUnit for backoffDelay

Returns:

The current WorkRequest.Builder

public final WorkRequest.Builder<B, W> setBackoffCriteria(BackoffPolicy backoffPolicy, java.time.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: The BackoffPolicy to use when increasing backoff time
duration: Time to wait before retrying the work

Returns:

The current WorkRequest.Builder

public final WorkRequest.Builder<B, W> setConstraints(Constraints constraints)

Adds constraints to the WorkRequest.

Parameters:

constraints: The constraints for the work

Returns:

The current WorkRequest.Builder

public final WorkRequest.Builder<B, W> 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: key/value pairs that will be provided to the worker

Returns:

The current WorkRequest.Builder

public final WorkRequest.Builder<B, W> addTag(java.lang.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: A tag for identifying the work in queries.

Returns:

The current WorkRequest.Builder

public final WorkRequest.Builder<B, W> keepResultsForAtLeast(long duration, java.util.concurrent.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: The minimum duration of time (in timeUnit units) to keep the results of this work
timeUnit: The unit of time for duration

Returns:

The current WorkRequest.Builder

public final WorkRequest.Builder<B, W> keepResultsForAtLeast(java.time.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 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: The minimum duration of time to keep the results of this work

Returns:

The current WorkRequest.Builder

public WorkRequest.Builder<B, W> setInitialDelay(long duration, java.util.concurrent.TimeUnit timeUnit)

Sets an initial delay for the WorkRequest.

Parameters:

duration: The length of the delay in timeUnit units
timeUnit: The units of time for duration

Returns:

The current WorkRequest.Builder

public WorkRequest.Builder<B, W> setInitialDelay(java.time.Duration duration)

Sets an initial delay for the WorkRequest.

Parameters:

duration: The length of the delay

Returns:

The current WorkRequest.Builder *

public WorkRequest.Builder<B, W> setExpedited(OutOfQuotaPolicy policy)

Marks the WorkRequest as important to the user. In this case, WorkManager provides an additional signal to the OS that this work is important.

Parameters:

policy: The OutOfQuotaPolicy to be used.

public final WorkRequest build()

Builds a WorkRequest based on this WorkRequest.Builder.

Returns:

A WorkRequest based on this WorkRequest.Builder

public final WorkRequest.Builder<B, W> setInitialState(WorkInfo.State state)

Sets the initial state for this work. Used in testing only.

Parameters:

state: The WorkInfo.State to set

Returns:

The current WorkRequest.Builder

public final WorkRequest.Builder<B, W> setInitialRunAttemptCount(int runAttemptCount)

Sets the initial run attempt count for this work. Used in testing only.

Parameters:

runAttemptCount: The initial run attempt count

Returns:

The current WorkRequest.Builder

public final WorkRequest.Builder<B, W> setLastEnqueueTime(long periodStartTime, java.util.concurrent.TimeUnit timeUnit)

Sets the period start time for this work. Used in testing only.

Parameters:

periodStartTime: the period start time in timeUnit units
timeUnit: The java.util.concurrent.TimeUnit for periodStartTime

Returns:

The current WorkRequest.Builder

public final WorkRequest.Builder<B, W> setScheduleRequestedAt(long scheduleRequestedAt, java.util.concurrent.TimeUnit timeUnit)

Sets when the scheduler actually schedules the worker.

Parameters:

scheduleRequestedAt: The time at which the scheduler scheduled a worker.
timeUnit: The java.util.concurrent.TimeUnit for scheduleRequestedAt

Returns:

The current WorkRequest.Builder