Services & Background Work Flashcards
ANDROID › Components
- Difference between a started service and a bound service?
- A started service is launched with startService/startForegroundService, runs independently until it calls stopSelf or is stopped via stopService. A bound service is created with bindService, returns an IBinder via onBind, and lives only while clients stay bound, dying once the last client unbinds.
- What thread does a Service run on, and why does it matter?
- By default a Service runs on the host process's main (UI) thread; it does not create its own thread. Blocking or long work there causes ANRs, so you must offload to a coroutine or background thread.
- What do START_STICKY, START_NOT_STICKY, and START_REDELIVER_INTENT mean?
- They are onStartCommand return values controlling restart after the system kills the service: START_STICKY recreates it with a null intent, START_NOT_STICKY does not recreate unless intents are pending, and START_REDELIVER_INTENT recreates and redelivers the last intent.
- What makes a foreground service a foreground service, and what does it require?
- It is a user-noticeable, long-running task that must show an ongoing notification. You start it via startForegroundService and call startForeground(id, notification) within ~5 seconds, declare a foregroundServiceType in the manifest, and hold FOREGROUND_SERVICE plus a type-specific permission.
- What changed for foreground services across Android 12, 13, and 14?
- Android 12 (API 31) restricts starting foreground services from the background; Android 13 (API 33) requires POST_NOTIFICATIONS and lets users dismiss the notification; Android 14 (API 34) requires every foreground service to declare a specific foregroundServiceType and meet that type's runtime preconditions.
- What is WorkManager and what kind of work is it for?
- WorkManager is the Jetpack library for deferrable, guaranteed background work that must survive app exit and device reboot, such as syncing or uploading logs. It persists work in a database and runs it under constraints, but is not for exact-time alarms or work that can die with the process.
- What scheduler does WorkManager use under the hood?
- It picks the best available API for the OS version: JobScheduler on API 23+, and on older versions a combination of AlarmManager and a BroadcastReceiver. You code against one API and it abstracts the backend.
- When should you use AlarmManager instead of WorkManager?
- Only when you need code to run at a precise wall-clock time, such as alarms or calendar reminders, using setExactAndAllowWhileIdle. For deferrable or recurring background work, WorkManager is preferred because it is battery-aware and persistent.
- What is the minimum interval for a PeriodicWorkRequest, and how do constraints help?
- The minimum repeat interval is 15 minutes. Constraints (e.g. unmetered network, charging, battery-not-low, device idle) let WorkManager defer execution until conditions are met, saving battery and data while still guaranteeing the work eventually runs.