WorkRequest.Builder
abstract class WorkRequest.Builder<B : WorkRequest.Builder<B, *>, W : 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 functions |
|
|---|---|
B |
Adds a tag for the work. |
W |
build()Builds a |
B |
@RequiresApi(value = 26)Specifies that the results of this work should be kept for at least the specified amount of time. |
B |
keepResultsForAtLeast(duration: Long, timeUnit: TimeUnit)Specifies that the results of this work should be kept for at least the specified amount of time. |
B |
@RequiresApi(value = 26)Sets the backoff policy and backoff delay for the work. |
B |
setBackoffCriteria(Sets the backoff policy and backoff delay for the work. |
B |
Specifies that the backoff policy (as specified via |
B |
setConstraints(constraints: Constraints)Adds constraints to the |
open B |
setExpedited(policy: OutOfQuotaPolicy)Marks the |
B |
Sets a unique identifier for this unit of work. |
open B |
@RequiresApi(value = 26)Sets an initial delay for the |
open B |
setInitialDelay(duration: Long, timeUnit: TimeUnit)Sets an initial delay for the |
B |
setInputData(inputData: Data)Adds input |
B |
setTraceTag(traceTag: String)Specifies the name of the trace span to be used by |
Public functions
addTag
fun addTag(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. |
| Returns | |
|---|---|
B |
The current |
build
fun build(): W
Builds a WorkRequest based on this Builder.
| Returns | |
|---|---|
W |
A |
keepResultsForAtLeast
@RequiresApi(value = 26)
fun keepResultsForAtLeast(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 |
| Returns | |
|---|---|
B |
The current |
keepResultsForAtLeast
fun keepResultsForAtLeast(duration: Long, 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: TimeUnit |
The unit of time for |
| Returns | |
|---|---|
B |
The current |
setBackoffCriteria
@RequiresApi(value = 26)
fun setBackoffCriteria(backoffPolicy: BackoffPolicy, duration: Duration): B
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 |
duration: Duration |
Time to wait before retrying the work |
| Returns | |
|---|---|
B |
The current |
setBackoffCriteria
fun setBackoffCriteria(
backoffPolicy: BackoffPolicy,
backoffDelay: Long,
timeUnit: TimeUnit
): B
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 |
backoffDelay: Long |
Time to wait before retrying the work in |
timeUnit: TimeUnit |
The |
| Returns | |
|---|---|
B |
The current |
setBackoffForSystemInterruptions
@ExperimentalWorkRequestBuilderApi
fun setBackoffForSystemInterruptions(): B
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.
| Returns | |
|---|---|
B |
The current |
| See also | |
|---|---|
setBackoffCriteria |
setConstraints
fun setConstraints(constraints: Constraints): B
Adds constraints to the WorkRequest.
| Parameters | |
|---|---|
constraints: Constraints |
The constraints for the work |
| Returns | |
|---|---|
B |
The current |
setExpedited
open fun setExpedited(policy: OutOfQuotaPolicy): B
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 | |
|---|---|
policy: OutOfQuotaPolicy |
The |
setId
fun setId(id: UUID): B
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.
| Parameters | |
|---|---|
id: UUID |
The unique identifier for this unit of work. |
| Returns | |
|---|---|
B |
The current |
setInitialDelay
@RequiresApi(value = 26)
open fun setInitialDelay(duration: Duration): B
Sets an initial delay for the WorkRequest.
| Parameters | |
|---|---|
duration: Duration |
The length of the delay |
| Returns | |
|---|---|
B |
The current |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given initial delay will push the execution time past |
setInitialDelay
open fun setInitialDelay(duration: Long, timeUnit: TimeUnit): B
Sets an initial delay for the WorkRequest.
| Parameters | |
|---|---|
duration: Long |
The length of the delay in |
timeUnit: TimeUnit |
The units of time for |
| Returns | |
|---|---|
B |
The current |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if the given initial delay will push the execution time past |
setInputData
fun setInputData(inputData: Data): B
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 |
setTraceTag
fun setTraceTag(traceTag: String): B
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.
| Parameters | |
|---|---|
traceTag: String |
The name of the trace tag, truncate to a |
| Returns | |
|---|---|
B |
The current |