Constraints


public final 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

public final class Constraints.Builder

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 fields

static final @NonNull Constraints

Represents a Constraints object with no requirements.

Public constructors

Constraints(
    @NonNull NetworkType requiredNetworkType,
    boolean requiresCharging,
    boolean requiresBatteryNotLow,
    boolean requiresStorageNotLow
)

Constructs Constraints.

@RequiresApi(value = 23)
Constraints(
    @NonNull NetworkType requiredNetworkType,
    boolean requiresCharging,
    boolean requiresDeviceIdle,
    boolean requiresBatteryNotLow,
    boolean requiresStorageNotLow
)

Constructs Constraints.

@RequiresApi(value = 24)
Constraints(
    @NonNull NetworkType requiredNetworkType,
    boolean requiresCharging,
    boolean requiresDeviceIdle,
    boolean requiresBatteryNotLow,
    boolean requiresStorageNotLow,
    long contentTriggerUpdateDelayMillis,
    long contentTriggerMaxDelayMillis,
    @NonNull Set<@NonNull Constraints.ContentUriTrigger> contentUriTriggers
)

Constructs Constraints.

Public methods

boolean
equals(Object other)
final long

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.

final long

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

final @NonNull Set<@NonNull Constraints.ContentUriTrigger>

Set of ContentUriTrigger.

final NetworkRequest

NetworkRequest required for work to run on.

final @NonNull NetworkType

The type of network required for the work to run.

int
final boolean
final boolean
final boolean
final boolean
@NonNull String

Public fields

NONE

public static final @NonNull Constraints NONE

Represents a Constraints object with no requirements.

Public constructors

Constraints

Added in 1.0.0
public Constraints(@NonNull Constraints other)

Constraints

Added in 2.9.0
public Constraints(
    @NonNull NetworkType requiredNetworkType,
    boolean requiresCharging,
    boolean requiresBatteryNotLow,
    boolean requiresStorageNotLow
)

Constructs Constraints.

Parameters
@NonNull NetworkType requiredNetworkType

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

boolean requiresCharging

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

boolean requiresBatteryNotLow

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

boolean requiresStorageNotLow

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)
public Constraints(
    @NonNull NetworkType requiredNetworkType,
    boolean requiresCharging,
    boolean requiresDeviceIdle,
    boolean requiresBatteryNotLow,
    boolean requiresStorageNotLow
)

Constructs Constraints.

Parameters
@NonNull NetworkType requiredNetworkType

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

boolean requiresCharging

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

boolean requiresDeviceIdle

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

boolean requiresBatteryNotLow

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

boolean requiresStorageNotLow

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)
public Constraints(
    @NonNull NetworkType requiredNetworkType,
    boolean requiresCharging,
    boolean requiresDeviceIdle,
    boolean requiresBatteryNotLow,
    boolean requiresStorageNotLow,
    long contentTriggerUpdateDelayMillis,
    long contentTriggerMaxDelayMillis,
    @NonNull Set<@NonNull Constraints.ContentUriTrigger> contentUriTriggers
)

Constructs Constraints.

Parameters
@NonNull NetworkType requiredNetworkType

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

boolean requiresCharging

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

boolean requiresDeviceIdle

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

boolean requiresBatteryNotLow

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

boolean requiresStorageNotLow

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

long 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. 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

long 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. This functionality is identical to the one found in JobScheduler and is described in android.app.job.JobInfo.Builder.setTriggerContentMaxDelay.

@NonNull Set<@NonNull Constraints.ContentUriTrigger> contentUriTriggers

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 methods

equals

public boolean equals(Object other)

getContentTriggerMaxDelayMillis

Added in 2.8.0
@RequiresApi(value = 24)
public final long getContentTriggerMaxDelayMillis()

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.

getContentTriggerUpdateDelayMillis

Added in 2.8.0
@RequiresApi(value = 24)
public final long getContentTriggerUpdateDelayMillis()

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

getContentUriTriggers

Added in 2.8.0
@RequiresApi(value = 24)
public final @NonNull Set<@NonNull Constraints.ContentUriTriggergetContentUriTriggers()

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.

getRequiredNetworkRequest

Added in 2.10.0
@RequiresApi(value = 21)
public final NetworkRequest getRequiredNetworkRequest()

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.

getRequiredNetworkType

Added in 1.0.0
public final @NonNull NetworkType getRequiredNetworkType()

The type of network required for the work to run.

hashCode

public int hashCode()

requiresBatteryNotLow

Added in 1.0.0
public final boolean requiresBatteryNotLow()
Returns
boolean

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

requiresCharging

Added in 1.0.0
public final boolean requiresCharging()
Returns
boolean

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

requiresDeviceIdle

Added in 1.0.0
@RequiresApi(value = 23)
public final boolean requiresDeviceIdle()
Returns
boolean

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

requiresStorageNotLow

Added in 1.0.0
public final boolean requiresStorageNotLow()
Returns
boolean

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

toString

public @NonNull String toString()