Expedited and long-running work

Background work and notifications Senior Kotlin 2.0+ API 31+ Reviewed By Best Latest Apps

Pick the fix

A 25-minute video upload worker keeps getting stopped. Best fix?

Options
Hint 1 of 1

What lets work run beyond about 10 minutes?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Call setForeground(...) with a dataSync notification to make it long-running

Correct. Long-running workers run in a foreground service WorkManager manages.

Why not the others?
  • B Mark it expedited. Expedited is for starting quickly, not running long.
  • C Split it into 25 periodic workers. Breaks the upload and adds overhead.
  • D Use a background Service. Background services are stopped quickly.

30-second answer

setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) asks the system to start the work right away, for short tasks the user cares about, like sending a message. It's limited by a quota. For work longer than about 10 minutes, a long-running worker calls setForeground() with a notification and a foreground service type, so it runs as a foreground service managed by WorkManager.

Practise saying it · 30 s. No recording, just a timer

Explain expedited and long-running work.

Say your answer out loud, then check which of these points you covered:

  • Expedited: start now, quota-limited
  • Long-running: setForeground
  • Normal workers stop after ~10 min
  • Foreground service type needed

Deep dive

On Android 12+, expedited work runs as a job; on older versions WorkManager uses a foreground service and needs getForegroundInfo().

Normal workers are stopped after about 10 minutes.

Long-running workers need the matching foreground service permission and type in the manifest.

User-initiated data transfer jobs (JobScheduler, Android 14) are another option for big user-started transfers.

Common mistakes

  • Marking all work expedited and exhausting the quota.
  • Forgetting getForegroundInfo for expedited work on old Android versions.
  • Running a 30-minute upload as a normal worker.

Senior take & follow-ups

Separate 'start now' (expedited) from 'run long' (setForeground).

Follow-up questions

Think of your answer first, then open the hint.

  1. What happens when the expedited quota runs out?

    Show hint

    With RUN_AS_NON_EXPEDITED, it runs as normal work.

  2. How long can a normal worker run?

    Show hint

    About 10 minutes before it's stopped.

Suggest a fix if something here is wrong or out of date.