0Pricing
Android Academy · Lesson

Constraints and Scheduling

Run work under the right conditions.

Constraints and Scheduling is a free Android Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Constraints and Scheduling

A Worker defines what to do. A WorkRequest defines when and under what conditions it runs. You build a request, attach constraints, and enqueue it.

What Constraints Are

Constraints are conditions that must be true before your work runs, such as having a network connection or being on charge. WorkManager waits until they are satisfied.

Building Constraints

Use Constraints.Builder to declare your requirements.

val constraints = Constraints.Builder()
    .setRequiredNetworkType(NetworkType.UNMETERED)
    .setRequiresCharging(true)
    .build()

Common Constraint Types

Frequently used constraints include:

  • setRequiredNetworkType (CONNECTED, UNMETERED, etc.)
  • setRequiresCharging
  • setRequiresBatteryNotLow
  • setRequiresDeviceIdle
  • setRequiresStorageNotLow

Building a OneTimeWorkRequest

For work that runs once, use OneTimeWorkRequestBuilder, parameterized with your worker class, and attach the constraints.

val request = OneTimeWorkRequestBuilder<UploadWorker>()
    .setConstraints(constraints)
    .build()

Passing Input Data

Attach input data to the request with setInputData; the worker reads it via inputData.

val request = OneTimeWorkRequestBuilder<UploadWorker>()
    .setInputData(workDataOf("user_id" to "42"))
    .setConstraints(constraints)
    .build()

Enqueuing the Work

Get the WorkManager instance and call enqueue. WorkManager persists the request and runs it when constraints are met.

WorkManager.getInstance(context).enqueue(request)

Backoff Policy

When a worker returns retry(), WorkManager waits before retrying. Configure the strategy with setBackoffCriteria, choosing linear or exponential backoff.

val request = OneTimeWorkRequestBuilder<UploadWorker>()
    .setBackoffCriteria(
        BackoffPolicy.EXPONENTIAL,
        10, TimeUnit.SECONDS
    )
    .build()

Unique Work

To avoid scheduling duplicate jobs, use enqueueUniqueWork with a name and a conflict policy (KEEP, REPLACE, or APPEND).

WorkManager.getInstance(context).enqueueUniqueWork(
    "sync_job",
    ExistingWorkPolicy.KEEP,
    request
)

Observing Work State

You can observe a request's status (ENQUEUED, RUNNING, SUCCEEDED, FAILED) via getWorkInfoByIdFlow or LiveData, useful for showing progress in the UI.

WorkManager.getInstance(context)
    .getWorkInfoByIdFlow(request.id)
    .collect { info -> /* react to info.state */ }

Adding Tags

Tag requests with addTag so you can query or cancel groups of related work by tag later, for example cancelling all "upload" work at once.

Quick Check

Test your understanding of constraints and scheduling.

Recap

You scheduled constrained work:

  • Constraints.Builder sets conditions like network and charging
  • OneTimeWorkRequestBuilder builds the request
  • enqueue / enqueueUniqueWork submits it
  • Configure backoff, observe state, and tag work

Next: periodic and chained work.

Frequently asked questions

Is the “Constraints and Scheduling” lesson free?

Yes — the full text of “Constraints and Scheduling” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Constraints and Scheduling”?

Run work under the right conditions. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Constraints and Scheduling” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. When to Use WorkManager
  2. Creating a Worker
  3. Constraints and Scheduling
  4. Periodic and Chained Work
← Back to Android Academy