Services (Foreground & Background)
📖 Concept
Services are components that run operations without a UI. At the senior level, understanding the OS restrictions, process priority, and WorkManager alternatives is critical.
Types of Services:
- Foreground Service — Visible to user (notification required). Higher process priority. Examples: music playback, navigation, file download.
- Background Service — No user-visible notification. Heavily restricted since Android 8.0 (Oreo). Killed aggressively by the OS.
- Bound Service — Provides a client-server interface. Lives only while clients are bound to it.
Background execution restrictions timeline:
- Android 6.0 (Doze): Background work paused when device idle
- Android 8.0: Cannot start background services from background. Must use startForegroundService() + show notification within 5 seconds.
- Android 12: Foreground service launch restrictions from background. Cannot start FGS from background except in specific cases.
- Android 14: Must declare foreground service type in manifest (camera, location, mediaPlayback, etc.)
Modern recommendation: Use WorkManager for deferrable background work. Use foreground services only for user-initiated, ongoing tasks.
Process priority (highest to lowest):
- Foreground process (Activity in RESUMED state)
- Visible process (Activity in STARTED, bound service from visible)
- Service process (foreground service running)
- Cached process (backgrounded, eligible for killing)
The OS kills processes from lowest to highest priority when memory is low. Understanding this hierarchy is essential for designing reliable background operations.
💻 Code Example
1// Foreground Service with Android 14+ requirements2class MusicPlayerService : Service() {3 override fun onCreate() {4 super.onCreate()5 createNotificationChannel()6 }78 override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {9 val notification = buildNotification()10 // Android 14+ requires foreground service type11 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {12 startForeground(NOTIFICATION_ID, notification,13 ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)14 } else {15 startForeground(NOTIFICATION_ID, notification)16 }17 startPlayback()18 return START_NOT_STICKY // Don't restart if killed19 }2021 override fun onBind(intent: Intent?): IBinder? = null2223 override fun onDestroy() {24 stopPlayback()25 super.onDestroy()26 }27}2829// Starting from Android 8.0+30// From foreground (Activity visible):31ContextCompat.startForegroundService(context,32 Intent(context, MusicPlayerService::class.java))3334// WorkManager for deferrable background work (preferred)35class SyncWorker(context: Context, params: WorkerParameters)36 : CoroutineWorker(context, params) {3738 override suspend fun doWork(): Result {39 return try {40 val repository = EntryPoints.get(applicationContext,41 SyncEntryPoint::class.java).repository()42 repository.sync()43 Result.success()44 } catch (e: Exception) {45 if (runAttemptCount < 3) Result.retry() else Result.failure()46 }47 }48}4950// Enqueue periodic sync51val syncRequest = PeriodicWorkRequestBuilder<SyncWorker>(52 15, TimeUnit.MINUTES53).setConstraints(54 Constraints.Builder()55 .setRequiredNetworkType(NetworkType.CONNECTED)56 .setRequiresBatteryNotLow(true)57 .build()58).setBackoffCriteria(59 BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES60).build()6162WorkManager.getInstance(context)63 .enqueueUniquePeriodicWork("sync", ExistingPeriodicWorkPolicy.KEEP, syncRequest)
🏋️ Practice Exercise
Practice:
- Implement a foreground service for file download with progress notification
- Explain the difference between START_STICKY, START_NOT_STICKY, and START_REDELIVER_INTENT
- Why can't you start a background service from Android 8.0+? What alternatives exist?
- Implement a WorkManager chain: download file → process → upload result
- Explain how bound services work and when you'd use them over other IPC mechanisms
⚠️ Common Mistakes
Not showing a notification within 5 seconds of startForegroundService() — causes ForegroundServiceDidNotStartInTimeException crash
Using background services for work that should use WorkManager — services are killed aggressively, WorkManager guarantees execution
Not declaring foreground service types in AndroidManifest on Android 14+ — runtime crash
Leaking bound service connections — always unbind in the appropriate lifecycle callback
💼 Interview Questions
🎤 Mock Interview
Mock interview is powered by AI for Services (Foreground & Background). Login to unlock this feature.