public abstract class WorkRequest.Builder<B extends WorkRequest.Builder<@NonNull B, @NonNull ?>, W extends WorkRequest>
A builder for WorkRequests. There are two concrete implementations of this class: OneTimeWorkRequest.Builder and PeriodicWorkRequest.Builder.
Summary
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.
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.
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.
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.
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.