Constraints

class Constraints


A specification of the requirements that need to be met before a WorkRequest can run. By default, WorkRequests do not have any requirements and can run immediately. By adding requirements, you can make sure that work only runs in certain situations - for example, when you have an unmetered network and are charging.

Summary

Nested types

A Builder for a Constraints object.

This class describes a content uri trigger on the WorkRequest: it should run when a local content: Uri is updated.

Public companion properties

Constraints

Represents a Constraints object with no requirements.

Public constructors

Constraints(
    requiredNetworkType: NetworkType,
    requiresCharging: Boolean,
    requiresBatteryNotLow: Boolean,
    requiresStorageNotLow: Boolean
)

Constructs Constraints.

@RequiresApi(value = 23)
Constraints(
    requiredNetworkType: NetworkType,
    requiresCharging: Boolean,
    requiresDeviceIdle: Boolean,
    requiresBatteryNotLow: Boolean,
    requiresStorageNotLow: Boolean
)

Constructs Constraints.

@RequiresApi(value = 24)
Constraints(
    requiredNetworkType: NetworkType,
    requiresCharging: Boolean,
    requiresDeviceIdle: Boolean,
    requiresBatteryNotLow: Boolean,
    requiresStorageNotLow: Boolean,
    contentTriggerUpdateDelayMillis: Long,
    contentTriggerMaxDelayMillis: Long,
    contentUriTriggers: Set<Constraints.ContentUriTrigger>
)

Constructs Constraints.

Public properties

Long
@ColumnInfo(name = "trigger_max_content_delay")
contentTriggerMaxDelayMillis

The maximum delay in milliseconds that is allowed from the first time a content: Uri change is detected to the time when the WorkRequest is scheduled.

Long
@ColumnInfo(name = "trigger_content_update_delay")
contentTriggerUpdateDelayMillis

The delay in milliseconds that is allowed from the time a content: Uri change is detected to the time when the WorkRequest is scheduled.

Set<Constraints.ContentUriTrigger>
@ColumnInfo(name = "content_uri_triggers")
contentUriTriggers

Set of ContentUriTrigger.

NetworkRequest?

NetworkRequest required for work to run on.

NetworkType
@ColumnInfo(name = "required_network_type")
requiredNetworkType

The type of network required for the work to run.

Public companion properties

NONE

val NONEConstraints

Represents a Constraints object with no requirements.

Public constructors

Constraints

Added in 1.0.0
Constraints(other: Constraints)

Constraints

Added in 2.9.0
Constraints(
    requiredNetworkType: NetworkType = NetworkType.NOT_REQUIRED,
    requiresCharging: Boolean = false,
    requiresBatteryNotLow: Boolean = false,
    requiresStorageNotLow: Boolean = false
)

Constructs Constraints.

Parameters
requiredNetworkType: NetworkType = NetworkType.NOT_REQUIRED

The type of network required for the work to run. The default value is NetworkType.NOT_REQUIRED.

requiresCharging: Boolean = false

whether device should be charging for the WorkRequest to run. The default value is false.

requiresBatteryNotLow: Boolean = false

whether device battery should be at an acceptable level for the WorkRequest to run. The default value is false.

requiresStorageNotLow: Boolean = false

whether the device's available storage should be at an acceptable level for the WorkRequest to run. The default value is false.

Constraints

Added in 2.9.0
@RequiresApi(value = 23)
Constraints(
    requiredNetworkType: NetworkType = NetworkType.NOT_REQUIRED,
    requiresCharging: Boolean = false,
    requiresDeviceIdle: Boolean = false,
    requiresBatteryNotLow: Boolean = false,
    requiresStorageNotLow: Boolean = false
)

Constructs Constraints.

Parameters
requiredNetworkType: NetworkType = NetworkType.NOT_REQUIRED

The type of network required for the work to run. The default value is NetworkType.NOT_REQUIRED.

requiresCharging: Boolean = false

whether device should be charging for the WorkRequest to run. The default value is false.

requiresDeviceIdle: Boolean = false

whether device should be idle for the WorkRequest to run. The default value is false.

requiresBatteryNotLow: Boolean = false

whether device battery should be at an acceptable level for the WorkRequest to run. The default value is false.

requiresStorageNotLow: Boolean = false

whether the device's available storage should be at an acceptable level for the WorkRequest to run. The default value is false.

Constraints

Added in 2.8.0
@RequiresApi(value = 24)
Constraints(
    requiredNetworkType: NetworkType = NetworkType.NOT_REQUIRED,
    requiresCharging: Boolean = false,
    requiresDeviceIdle: Boolean = false,
    requiresBatteryNotLow: Boolean = false,
    requiresStorageNotLow: Boolean = false,
    contentTriggerUpdateDelayMillis: Long = -1,
    contentTriggerMaxDelayMillis: Long = -1,
    contentUriTriggers: Set<Constraints.ContentUriTrigger> = setOf()
)

Constructs Constraints.

Parameters
requiredNetworkType: NetworkType = NetworkType.NOT_REQUIRED

The type of network required for the work to run. The default value is NetworkType.NOT_REQUIRED.

requiresCharging: Boolean = false

whether device should be charging for the WorkRequest to run. The default value is false.

requiresDeviceIdle: Boolean = false

whether device should be idle for the WorkRequest to run. The default value is false.

requiresBatteryNotLow: Boolean = false

whether device battery should be at an acceptable level for the WorkRequest to run. The default value is false.

requiresStorageNotLow: Boolean = false

whether the device's available storage should be at an acceptable level for the WorkRequest to run. The default value is false.

contentTriggerUpdateDelayMillis: Long = -1

the delay in milliseconds that is allowed from the time a content: Uri change is detected to the time when the WorkRequest is scheduled. If there are more changes during this time, the delay will be reset to the start of the most recent change. This functionality is identical to the one found in JobScheduler and is described in android.app.job.JobInfo.Builder.setTriggerContentUpdateDelay

contentTriggerMaxDelayMillis: Long = -1

the maximum delay in milliseconds that is allowed from the first time a content: Uri change is detected to the time when the WorkRequest is scheduled. This functionality is identical to the one found in JobScheduler and is described in android.app.job.JobInfo.Builder.setTriggerContentMaxDelay.

contentUriTriggers: Set<Constraints.ContentUriTrigger> = setOf()

set of ContentUriTrigger. WorkRequest will run when a local content: Uri of one of the triggers in the set is updated. This functionality is identical to the one found in JobScheduler and is described in android.app.job.JobInfo.Builder.addTriggerContentUri.

Public functions

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

requiresBatteryNotLow

Added in 1.0.0
fun requiresBatteryNotLow(): Boolean
Returns
Boolean

true if the work should only execute when the battery isn't low

requiresCharging

Added in 1.0.0
fun requiresCharging(): Boolean
Returns
Boolean

true if the work should only execute while the device is charging

requiresDeviceIdle

Added in 1.0.0
@RequiresApi(value = 23)
fun requiresDeviceIdle(): Boolean
Returns
Boolean

true if the work should only execute while the device is idle

requiresStorageNotLow

Added in 1.0.0
fun requiresStorageNotLow(): Boolean
Returns
Boolean

true if the work should only execute when the storage isn't low

toString

open fun toString(): String

Public properties

contentTriggerMaxDelayMillis

Added in 2.8.0
@ColumnInfo(name = "trigger_max_content_delay")
val contentTriggerMaxDelayMillisLong

The maximum delay in milliseconds that is allowed from the first time a content: Uri change is detected to the time when the WorkRequest is scheduled. This functionality is identical to the one found in JobScheduler and is described in android.app.job.JobInfo.Builder.setTriggerContentMaxDelay.

contentTriggerUpdateDelayMillis

Added in 2.8.0
@ColumnInfo(name = "trigger_content_update_delay")
val contentTriggerUpdateDelayMillisLong

The delay in milliseconds that is allowed from the time a content: Uri change is detected to the time when the WorkRequest is scheduled. If there are more changes during this time, the delay will be reset to the start of the most recent change. This functionality is identical to the one found in JobScheduler and is described in android.app.job.JobInfo.Builder.setTriggerContentUpdateDelay

contentUriTriggers

Added in 2.8.0
@ColumnInfo(name = "content_uri_triggers")
val contentUriTriggersSet<Constraints.ContentUriTrigger>

Set of ContentUriTrigger. WorkRequest will run when a local content: Uri of one of the triggers in the set is updated. This functionality is identical to the one found in JobScheduler and is described in android.app.job.JobInfo.Builder.addTriggerContentUri.

requiredNetworkRequest

val requiredNetworkRequestNetworkRequest?

NetworkRequest required for work to run on. It is used only the API levels >= 28 (Android P). For the older API levels, requiredNetworkType will be used instead on the older platforms and this property will be null.

NetworkRequest-s with NetworkSpecifier set aren't supported, as well as NetworkRequest with setIncludeOtherUidNetworks set. passed.

requiredNetworkType

Added in 1.0.0
@ColumnInfo(name = "required_network_type")
val requiredNetworkTypeNetworkType

The type of network required for the work to run.