Stay organized with collections Save and categorize content based on your preferences.

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.

public static class Constraints.Companion

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

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 @NonNull NetworkType

The type of network required for the work to run.

int
final boolean
final boolean
final boolean
final boolean

Public fields

NONE

public static final @NonNull Constraints NONE

Represents a Constraints object with no requirements.

Public constructors

Constraints

public Constraints(@NonNull Constraints other)

Constraints

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

Public methods

equals

public boolean equals(Object other)

getContentTriggerMaxDelayMillis

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

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

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.

getRequiredNetworkType

public final @NonNull NetworkType getRequiredNetworkType()

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

hashCode

public int hashCode()

requiresBatteryNotLow

public final boolean requiresBatteryNotLow()
Returns
boolean

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

requiresCharging

public final boolean requiresCharging()
Returns
boolean

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

requiresDeviceIdle

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

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

requiresStorageNotLow

public final boolean requiresStorageNotLow()
Returns
boolean

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