Android Academy · Lesson

Constraints and Scheduling

Run work under the right conditions.

Lesson 3 of 413 steps

Constraints and Scheduling is a free Android Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

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.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “Constraints and Scheduling” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Constraints and Scheduling”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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