WorkRequest.Builder
public abstract class WorkRequest.Builder<B extends WorkRequest.Builder<@NonNull B, @NonNull ?>, W extends WorkRequest>
OneTimeWorkRequest.Builder |
Builder for |
PeriodicWorkRequest.Builder |
Builder for |
A builder for WorkRequests. There are two concrete implementations of this class: OneTimeWorkRequest.Builder and PeriodicWorkRequest.Builder.
Summary
Public methods |
|
|---|---|
final @NonNull B |
Adds a tag for the work. |
final @NonNull W |
build()Builds a |
final @NonNull B |
@RequiresApi(value = 26)Specifies that the results of this work should be kept for at least the specified amount of time. |
final @NonNull B |
keepResultsForAtLeast(long duration, @NonNull TimeUnit timeUnit)Specifies that the results of this work should be kept for at least the specified amount of time. |
final @NonNull B |
@RequiresApi(value = 26)Sets the backoff policy and backoff delay for the work. |
final @NonNull B |
setBackoffCriteria(Sets the backoff policy and backoff delay for the work. |
final @NonNull B |
Specifies that the backoff policy (as specified via |
final @NonNull B |
setConstraints(@NonNull Constraints constraints)Adds constraints to the |
@NonNull B |
setExpedited(@NonNull OutOfQuotaPolicy policy)Marks the |
final @NonNull B |
Sets a unique identifier for this unit of work. |
@NonNull B |
@RequiresApi(value = 26)Sets an initial delay for the |
@NonNull B |
setInitialDelay(long duration, @NonNull TimeUnit timeUnit)Sets an initial delay for the |
final @NonNull B |
setInputData(@NonNull Data inputData)Adds input |
final @NonNull B |
setTraceTag(@NonNull String traceTag)Specifies the name of the trace span to be used by |
Public methods
addTag
public final @NonNull B addTag(@NonNull 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.
build
public final @NonNull W build()
Builds a WorkRequest based on this Builder.
| Returns | |
|---|---|
@NonNull W |
A |
keepResultsForAtLeast
@RequiresApi(value = 26)
public final @NonNull B keepResultsForAtLeast(@NonNull 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.
keepResultsForAtLeast
public final @NonNull B keepResultsForAtLeast(long duration, @NonNull 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.
setBackoffCriteria
@RequiresApi(value = 26)
public final @NonNull B setBackoffCriteria(
@NonNull BackoffPolicy backoffPolicy,
@NonNull 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 | |
|---|---|
@NonNull BackoffPolicy backoffPolicy |
The |
@NonNull Duration duration |
Time to wait before retrying the work |
setBackoffCriteria
public final @NonNull B setBackoffCriteria(
@NonNull BackoffPolicy backoffPolicy,
long backoffDelay,
@NonNull 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 | |
|---|---|
@NonNull BackoffPolicy backoffPolicy |
The |
long backoffDelay |
Time to wait before retrying the work in |
@NonNull TimeUnit timeUnit |
The |
setBackoffForSystemInterruptions
@ExperimentalWorkRequestBuilderApi
public final @NonNull B setBackoffForSystemInterruptions()
Specifies that the backoff policy (as specified via setBackoffCriteria) will be applied when work is interrupted by the system without the app requesting it. This might happen when the ListenableWorker runs longer than it should, or when constraints defined for a given ListenableWorker are unmet.
| See also | |
|---|---|
setBackoffCriteria |
setConstraints
public final @NonNull B setConstraints(@NonNull Constraints constraints)
Adds constraints to the WorkRequest.
| Parameters | |
|---|---|
@NonNull Constraints constraints |
The constraints for the work |
setExpedited
public @NonNull B setExpedited(@NonNull 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.
Note that although the execution time of this work won't be counted against your app's quota while your app is in the foreground, if the expedited work continues in the background, you are susceptible to quota. However, power management restrictions, such as Battery Saver and Doze, are less likely to affect expedited work. Because of this, expedited work is best suited for short tasks which need to start immediately and are important to the user or user-initiated.
| Parameters | |
|---|---|
@NonNull OutOfQuotaPolicy policy |
The |
setId
public final @NonNull B setId(@NonNull UUID id)
Sets a unique identifier for this unit of work.
The id can be useful when retrieving WorkInfo by id or when trying to update an existing work. For example, using WorkManager.updateWork requires that the work has an id.
setInitialDelay
@RequiresApi(value = 26)
public @NonNull B setInitialDelay(@NonNull Duration duration)
Sets an initial delay for the WorkRequest.
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given initial delay will push the execution time past |
setInitialDelay
public @NonNull B setInitialDelay(long duration, @NonNull TimeUnit timeUnit)
Sets an initial delay for the WorkRequest.
| Parameters | |
|---|---|
long duration |
The length of the delay in |
@NonNull TimeUnit timeUnit |
The units of time for |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given initial delay will push the execution time past |
setInputData
public final @NonNull B setInputData(@NonNull 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.
setTraceTag
public final @NonNull B setTraceTag(@NonNull String traceTag)
Specifies the name of the trace span to be used by WorkManager when executing the specified WorkRequest.
WorkManager uses the simple name of the ListenableWorker class truncated to a 127 character string, as the traceTag by default.
You should override the traceTag, when you are using ListenableWorker delegation via a WorkerFactory.