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 @NonNulladdTag(@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.

Parameters
@NonNull String tag

A tag for identifying the work in queries.

keepResultsForAtLeast

@RequiresApi(value = 26)
public final @NonNullkeepResultsForAtLeast(@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.

Parameters
@NonNull Duration duration

The minimum duration of time to keep the results of this work

keepResultsForAtLeast

public final @NonNullkeepResultsForAtLeast(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.

Parameters
long duration

The minimum duration of time (in timeUnit units) to keep the results of this work

@NonNull TimeUnit timeUnit

The unit of time for duration

setExpedited

public @NonNullsetExpedited(@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 @NonNullsetId(@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.

Parameters
@NonNull UUID id

The unique identifier for this unit of work.

setInputData

public final @NonNullsetInputData(@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.

Parameters
@NonNull Data inputData

key/value pairs that will be provided to the worker

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2026-03-25 UTC.