Services & Background Work Quiz

ANDROID › Components

You need to upload analytics logs to your server. The upload should be reliable, can wait for Wi-Fi, and must survive the app being killed or the device rebooting. What should you use?

Answer: WorkManager with a network constraint

Deferrable, guaranteed work that must persist across process death and reboot, optionally gated on network type, is exactly WorkManager's job; it persists requests and reschedules after reboot.

A Service's onStartCommand returns START_STICKY. The system kills the service under memory pressure with no pending start intents. What happens?

Answer: The service is recreated and onStartCommand gets a null intent

START_STICKY recreates the service after a kill and calls onStartCommand with a null intent when there are no pending intents, which is why sticky services must tolerate a null intent.

Targeting Android 14 (API 34), which is now required to legally run a foreground service?

Answer: Declaring a specific foregroundServiceType in the manifest and meeting its rules

Since API 34, every foreground service must declare a specific foregroundServiceType in the manifest and satisfy that type's runtime requirements and permissions, or it throws at runtime.

Which statement about a default Service's threading is correct?

Answer: A Service runs on the app's main thread and can cause ANRs

A Service does not create its own thread or process; it runs on the application's main thread, so blocking work must be moved off it to avoid ANRs.

Why is a plain background Service started with startService() discouraged on modern Android?

Answer: API 26+ limits background services when the app isn’t foregrounded

Starting with API 26, the system restricts background services while the app is not in the foreground, which is why WorkManager or a foreground service is preferred for ongoing background work.

Which task is the correct fit for AlarmManager rather than WorkManager?

Answer: Firing a user-facing alarm clock at an exact time

AlarmManager is for precise wall-clock execution such as alarms and reminders; deferrable, constraint-based, or retrying background work belongs to WorkManager.

What is the minimum repeat interval allowed for a PeriodicWorkRequest in WorkManager?

Answer: 15 minutes, the minimum periodic interval WorkManager enforces

WorkManager caps periodic work at a minimum interval of 15 minutes to protect battery; requesting less is clamped up to 15 minutes.

A Service is only ever connected to via bindService() with BIND_AUTO_CREATE and is never started with startService(). When does the system destroy it?

Answer: When the last bound client unbinds or is itself destroyed

A purely bound service lives only as long as at least one client is bound; once the final client calls unbindService (or dies), the system tears it down. stopSelf applies to started services, not bound-only ones.

A started service does critical work driven by the start intent's data, and you want the system to re-run that exact job if it kills the service before the work finishes. Which onStartCommand return value should you use?

Answer: START_REDELIVER_INTENT

START_REDELIVER_INTENT recreates the service after a kill and redelivers the last intent, so work tied to that intent's data is retried. START_STICKY redelivers a null intent, and START_NOT_STICKY does not restart at all.

An app calls startForegroundService() but the service never calls startForeground() within the roughly 5-second window the system allows. What is the typical result?

Answer: The system throws a timeout exception and may ANR or crash the app

After startForegroundService, the app must promote the service with startForeground within about 5 seconds or the system raises a timeout (ForegroundServiceDidNotStartInTimeException), which can ANR or crash the process.

On Android 12 (API 31), a non-exempt app that is currently in the background tries to start a foreground service. What typically happens?

Answer: It throws a ForegroundServiceStartNotAllowedException

API 31 blocks starting foreground services from the background in most cases, throwing ForegroundServiceStartNotAllowedException; you must use an allowed exemption or defer the work via WorkManager/expedited work instead.

Inside a WorkManager worker you need to call suspend functions and other coroutine-based APIs. Which base class is designed for that?

Answer: CoroutineWorker, overriding its suspend doWork()

CoroutineWorker exposes a suspend doWork() that runs in a coroutine (Dispatchers.Default by default) and is cancelled cooperatively when the work stops, making it the idiomatic choice for suspend-based work.

Which statement about WorkManager and JobScheduler is correct on modern Android (API 23 and above)?

Answer: WorkManager uses JobScheduler internally, so you target WorkManager, not JobScheduler

On API 23+ WorkManager uses JobScheduler as its backing scheduler while adding persistence, constraints, chaining, and a single consistent API, so app code targets WorkManager instead of JobScheduler directly.

IntentService is deprecated. For the serialized background work it used to handle, what is the recommended modern replacement?

Answer: Use WorkManager, or JobIntentService, since background services are now restricted

IntentService relied on background-service behavior that API 26+ restricts, so Google recommends WorkManager for deferrable guaranteed work (or JobIntentService as a transitional bridge) instead of resurrecting a background Service.

Back to Services & Background Work