Configuration.Builder


class Configuration.Builder


A Builder for Configurations.

Summary

Public constructors

Creates a new Configuration.Builder.

Public functions

Configuration

Builds a Configuration object.

Configuration.Builder
setClock(clock: Clock)

Sets a Clock for WorkManager to calculate schedules and perform book-keeping.

Configuration.Builder
setContentUriTriggerWorkersLimit(contentUriTriggerWorkersLimit: Int)

Specifies the maximum number of Workers with Constraints.contentUriTriggers that could be enqueued simultaneously.

Configuration.Builder

Designates the primary process that WorkManager should schedule work in.

Configuration.Builder
setExecutor(executor: Executor)

Specifies a custom Executor to run Worker.doWork.

Configuration.Builder

Specifies a Consumer<Throwable> that can be used to intercept exceptions caused when trying to initialize {@link WorkManager}, that usually happens when WorkManager cannot access its internal datastore.

Configuration.Builder

Specifies a custom InputMergerFactory for WorkManager.

Configuration.Builder
setJobSchedulerJobIdRange(minJobSchedulerId: Int, maxJobSchedulerId: Int)

Specifies the range of android.app.job.JobInfo IDs that can be used by WorkManager.

Configuration.Builder

Regulates whether WorkManager should automatically set android.app.job.JobInfo.Builder.setImportantWhileForeground for workers that are eligible to run immediately.

Configuration.Builder
setMaxSchedulerLimit(maxSchedulerLimit: Int)

Specifies the maximum number of system requests made by WorkManager when using android.app.job.JobScheduler or android.app.AlarmManager.

Configuration.Builder
setMinimumLoggingLevel(loggingLevel: Int)

Specifies the minimum logging level, corresponding to the constants found in android.util.Log.

Configuration.Builder

Specifies the RunnableScheduler to be used by WorkManager.

Configuration.Builder
setSchedulingExceptionHandler(
    schedulingExceptionHandler: Consumer<Throwable>
)

Specifies a Consumer<Throwable> that can be used to intercept exceptions caused when trying to schedule WorkRequests.

Configuration.Builder
setTaskExecutor(taskExecutor: Executor)

Specifies a Executor which will be used by WorkManager for all its internal book-keeping.

Configuration.Builder

Specifies a custom CoroutineContext to run CoroutineWorker.doWork.

Configuration.Builder

Specifies a WorkerExceptionHandler that can be used to intercept exceptions caused when trying to execute ListenableWorkers.

Configuration.Builder

Specifies a custom WorkerFactory for WorkManager.

Configuration.Builder

Specifies a WorkerExceptionHandler that can be used to intercept exceptions caused when trying to initialize ListenableWorkers.

Public constructors

Builder

Added in 1.0.0
Builder()

Creates a new Configuration.Builder.

Public functions

build

Added in 1.0.0
fun build(): Configuration

Builds a Configuration object.

Returns
Configuration

A Configuration object with this Builder's parameters.

setClock

Added in 2.9.0
fun setClock(clock: Clock): Configuration.Builder

Sets a Clock for WorkManager to calculate schedules and perform book-keeping.

This should only be overridden for testing. It must return the same value as System.currentTimeMillis in production code.

Parameters
clock: Clock

The Clock to use

Returns
Configuration.Builder

This Builder instance

setContentUriTriggerWorkersLimit

Added in 2.9.0
fun setContentUriTriggerWorkersLimit(contentUriTriggerWorkersLimit: Int): Configuration.Builder

Specifies the maximum number of Workers with Constraints.contentUriTriggers that could be enqueued simultaneously.

Unlike the other workers Workers with Constraints.contentUriTriggers must immediately occupy slots in JobScheduler to avoid missing updates, thus they are separated in its own category.

setDefaultProcessName

Added in 2.5.0
fun setDefaultProcessName(processName: String): Configuration.Builder

Designates the primary process that WorkManager should schedule work in.

Parameters
processName: String

The String process name.

Returns
Configuration.Builder

This Builder instance

setExecutor

Added in 1.0.0
fun setExecutor(executor: Executor): Configuration.Builder

Specifies a custom Executor to run Worker.doWork.

If setWorkerCoroutineContext wasn't called then the executor will be used as CoroutineDispatcher to run CoroutineWorker as well.

Parameters
executor: Executor

An Executor for running Workers

Returns
Configuration.Builder

This Builder instance

setInitializationExceptionHandler

Added in 2.8.0
fun setInitializationExceptionHandler(
    exceptionHandler: Consumer<Throwable>
): Configuration.Builder

Specifies a Consumer<Throwable> that can be used to intercept exceptions caused when trying to initialize {@link WorkManager}, that usually happens when WorkManager cannot access its internal datastore.

This exception handler will be invoked on a thread bound to Configuration.taskExecutor.

Parameters
exceptionHandler: Consumer<Throwable>

an instance to handle exceptions

Returns
Configuration.Builder

This Builder instance

setInputMergerFactory

Added in 2.3.0
fun setInputMergerFactory(inputMergerFactory: InputMergerFactory): Configuration.Builder

Specifies a custom InputMergerFactory for WorkManager.

Parameters
inputMergerFactory: InputMergerFactory

A InputMergerFactory for creating InputMergers

Returns
Configuration.Builder

This Builder instance

setJobSchedulerJobIdRange

Added in 1.0.0
fun setJobSchedulerJobIdRange(minJobSchedulerId: Int, maxJobSchedulerId: Int): Configuration.Builder

Specifies the range of android.app.job.JobInfo IDs that can be used by WorkManager. WorkManager needs a range of at least 1000 IDs.

JobScheduler uses integers as identifiers for jobs, and WorkManager delegates to JobScheduler on certain API levels. In order to not clash job codes used in the rest of your app, you can use this method to tell WorkManager the valid range of job IDs that it can use.

The default values are 0 and Integer#MAX_VALUE.

Parameters
minJobSchedulerId: Int

The first valid android.app.job.JobInfo ID (inclusive).

maxJobSchedulerId: Int

The last valid android.app.job.JobInfo ID (inclusive).

Returns
Configuration.Builder

This Builder instance

Throws
kotlin.IllegalArgumentException

when the size of the range is less than 1000

setMarkingJobsAsImportantWhileForeground

Added in 2.10.0
@ExperimentalConfigurationApi
fun setMarkingJobsAsImportantWhileForeground(markAsImportant: Boolean): Configuration.Builder

Regulates whether WorkManager should automatically set android.app.job.JobInfo.Builder.setImportantWhileForeground for workers that are eligible to run immediately.

It will have effects only on API levels >= 23.

Parameters
markAsImportant: Boolean

whether to mark jobs as important

Returns
Configuration.Builder

This Builder instance

setMaxSchedulerLimit

Added in 1.0.0
fun setMaxSchedulerLimit(maxSchedulerLimit: Int): Configuration.Builder

Specifies the maximum number of system requests made by WorkManager when using android.app.job.JobScheduler or android.app.AlarmManager.

By default, WorkManager might schedule a large number of alarms or JobScheduler jobs. If your app uses JobScheduler or AlarmManager directly, this might exhaust the OS-enforced limit on the number of jobs or alarms an app is allowed to schedule. To help manage this situation, you can use this method to reduce the number of underlying jobs and alarms that WorkManager might schedule.

When the application exceeds this limit, WorkManager maintains an internal queue of WorkRequests, and schedules them when slots become free.

WorkManager requires a minimum of Configuration.MIN_SCHEDULER_LIMIT slots; this is also the default value. The total number of slots also cannot exceed 50.

Parameters
maxSchedulerLimit: Int

The total number of jobs which can be enqueued by WorkManager when using android.app.job.JobScheduler.

Returns
Configuration.Builder

This Builder instance

Throws
kotlin.IllegalArgumentException

if maxSchedulerLimit is less than Configuration.MIN_SCHEDULER_LIMIT

setMinimumLoggingLevel

Added in 1.0.0
fun setMinimumLoggingLevel(loggingLevel: Int): Configuration.Builder

Specifies the minimum logging level, corresponding to the constants found in android.util.Log. For example, specifying android.util.Log.VERBOSE will log everything, whereas specifying android.util.Log.ERROR will only log errors and assertions.The default value is android.util.Log.INFO.

Parameters
loggingLevel: Int

The minimum logging level, corresponding to the constants found in android.util.Log

Returns
Configuration.Builder

This Builder instance

setRunnableScheduler

Added in 2.4.0
fun setRunnableScheduler(runnableScheduler: RunnableScheduler): Configuration.Builder

Specifies the RunnableScheduler to be used by WorkManager.

This is used by the in-process scheduler to keep track of timed work.

Parameters
runnableScheduler: RunnableScheduler

The RunnableScheduler to be used

Returns
Configuration.Builder

This Builder instance

setSchedulingExceptionHandler

Added in 2.8.0
fun setSchedulingExceptionHandler(
    schedulingExceptionHandler: Consumer<Throwable>
): Configuration.Builder

Specifies a Consumer<Throwable> that can be used to intercept exceptions caused when trying to schedule WorkRequests.

It allows the application to handle a Throwable throwable typically caused when trying to schedule WorkRequests.

This exception handler will be invoked on a thread bound to Configuration.taskExecutor.

Parameters
schedulingExceptionHandler: Consumer<Throwable>

an instance to handle exceptions

Returns
Configuration.Builder

This Builder instance

setTaskExecutor

Added in 2.1.0
fun setTaskExecutor(taskExecutor: Executor): Configuration.Builder

Specifies a Executor which will be used by WorkManager for all its internal book-keeping.

For best performance this Executor should be bounded.

For more information look at androidx.room.RoomDatabase.Builder.setQueryExecutor.

Parameters
taskExecutor: Executor

The Executor which will be used by WorkManager for all its internal book-keeping

Returns
Configuration.Builder

This Builder instance

setWorkerCoroutineContext

Added in 2.10.0
fun setWorkerCoroutineContext(context: CoroutineContext): Configuration.Builder

Specifies a custom CoroutineContext to run CoroutineWorker.doWork. WorkManager will use its own Job with the provided CoroutineContext.

If setExecutor wasn't called then context will be used as Executor to run Worker as well.

Parameters
context: CoroutineContext

A CoroutineContext for running CoroutineWorkers

Returns
Configuration.Builder

This Builder instance

setWorkerExecutionExceptionHandler

Added in 2.10.0
fun setWorkerExecutionExceptionHandler(
    workerExceptionHandler: Consumer<WorkerExceptionInfo>
): Configuration.Builder

Specifies a WorkerExceptionHandler that can be used to intercept exceptions caused when trying to execute ListenableWorkers.

This exception handler will be invoked on a thread bound to Configuration.taskExecutor.

Parameters
workerExceptionHandler: Consumer<WorkerExceptionInfo>

an instance to handle exceptions

Returns
Configuration.Builder

This Builder instance

setWorkerFactory

Added in 1.0.0
fun setWorkerFactory(workerFactory: WorkerFactory): Configuration.Builder

Specifies a custom WorkerFactory for WorkManager.

Parameters
workerFactory: WorkerFactory

A WorkerFactory for creating ListenableWorkers

Returns
Configuration.Builder

This Builder instance

setWorkerInitializationExceptionHandler

Added in 2.10.0
fun setWorkerInitializationExceptionHandler(
    workerExceptionHandler: Consumer<WorkerExceptionInfo>
): Configuration.Builder

Specifies a WorkerExceptionHandler that can be used to intercept exceptions caused when trying to initialize ListenableWorkers.

This exception handler will be invoked on a thread bound to Configuration.taskExecutor.

Parameters
workerExceptionHandler: Consumer<WorkerExceptionInfo>

an instance to handle exceptions

Returns
Configuration.Builder

This Builder instance