tezvyn:

Android 12 foreground service restrictions and WorkManager expedited jobs

Curated by the Tezvyn teamSource: developer.android.comadvanced
Android 12 foreground service restrictions and WorkManager expedited jobs

Tests Android 12 background-start limits. A great answer covers: the API 31 ban on background FGS starts, BOOT_COMPLETED exceptions, and WorkManager setExpedited with out-of-quota fallback. Red flag: starting an FGS in a BroadcastReceiver ignoring quota.

WHAT THIS TESTS: This question probes your depth on Android 12 behavior changes for background execution, specifically the restriction on starting foreground services from background contexts. Interviewers want to see that you know the modern replacement API, WorkManager expedited jobs, and that you understand quota limits rather than treating background work as unlimited.

A GOOD ANSWER COVERS: First, state that Android 12 (API level 31) generally prohibits apps from starting foreground services when running in the background, with only a few narrow exceptions such as receiving a BOOT_COMPLETED broadcast or a high-priority Firebase Cloud Message. Second, explain that WorkManager offers expedited work via setExpedited, which internally runs as a foreground service but is managed by the system and subject to quota limits. Third, mention the OutOfQuotaPolicy parameter, specifically RUN_AS_NON_EXPEDITED_WORK_REQUEST, which defines graceful degradation when the app exceeds its expedited job quota. Fourth, describe the BroadcastReceiver scenario: instead of calling startForegroundService directly from onReceive, you enqueue a OneTimeWorkRequest built with setExpedited and an appropriate fallback policy, letting WorkManager handle the service lifecycle and notification.

COMMON WRONG ANSWERS: A major red flag is proposing to start a raw foreground service directly from a BroadcastReceiver without acknowledging the API 31 restriction. Another weak pattern is mentioning JobScheduler instead of WorkManager, since the question explicitly asks for WorkManager's expedited jobs. Candidates who say expedited jobs are identical to normal foreground services also lose points; the interviewer expects you to note the quota system and automatic fallback behavior.

LIKELY FOLLOW-UPS: The interviewer may ask how expedited job quotas are calculated, typically based on app standby buckets and a rolling window. They might also ask what happens if you target Android 13 or 14, where notification permissions and additional foreground service types further constrain behavior. Another common follow-up is how to test this in a unit test or integration test, or how to handle the notification requirement when WorkManager manages the foreground service notification for you.

ONE CONCRETE EXAMPLE: Suppose your app receives a BOOT_COMPLETED broadcast and must immediately sync critical configuration. Prior to Android 12 you might have called startForegroundService from the receiver. On API 31 and above, you instead create a OneTimeWorkRequest, call setExpedited with OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST, and enqueue it with WorkManager. If your app has quota, WorkManager promotes it to an expedited job running as a foreground service; if quota is exhausted, it falls back to regular work and completes later without crashing or violating platform restrictions.

Source: developer.android.com

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.