tezvyn:

Expedited Work: High-Priority Background Tasks in Android

AI-drafted, machine-checkedSource: developer.android.comadvanced
Expedited Work: High-Priority Background Tasks in Android

Expedited work is for important, short tasks that must run quickly. It gets higher priority than regular background work without requiring a persistent notification. Use it for sending messages or saving notes.

WHY IT EXISTS: Android aggressively manages background processing to save battery. Standard background work can be heavily delayed. Foreground services get priority but require a persistent, user-visible notification, which is intrusive for short tasks. Expedited work was created for tasks that need to run immediately but finish too quickly to justify a foreground service.

THE MENTAL MODEL: Think of expedited work as a priority pass for a background task. Normally, your task waits in a long, slow-moving queue (regular background jobs). A foreground service lets you cut to the front of the line, but you have to wear a big, flashing sign (the notification). Expedited work lets you use a special, faster queue without the sign, but there's a limited number of passes available.

HOW IT WORKS: Within Android's WorkManager library, you create a WorkRequest and designate it as expedited by calling .setExpedited(). The system then attempts to run the job as soon as its constraints are met, without the delays typical of standard background work. To achieve this, WorkManager may manage a short-lived foreground service on your behalf, but this is an implementation detail you don't manage. The system grants each app a limited time quota to run these jobs.

WHEN TO USE IT: Use expedited work for user-initiated tasks that are short (a few minutes at most), cannot be deferred, and should complete even if the user leaves the app. Good examples include: sending a chat message the user just typed, processing a quick payment, or saving a newly created note to a remote database.

WHEN NOT TO USE IT: Do not use it for tasks that can be deferred, like syncing data periodically or pre-fetching content. It is also unsuitable for long-running processes like downloading large files or transcoding media; use a full foreground service for those. If a task must run at an exact time, use AlarmManager instead.

ONE CANONICAL EXAMPLE: A user in a messaging app hits "send" on a message. The app schedules an expedited OneTimeWorkRequest to upload the content. The user can navigate away, and the work will still execute promptly in the background. If the system's expedited work quota is exhausted, the upload will still happen, but it will be demoted and run as a regular, lower-priority background job.

Read the original → developer.android.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.