A Service is an Android component that runs operations in the background without a UI. BroadcastReceivers listen for system-wide or app-specific events and react to them.
Activity (UI) → User-facing screens
Service (no UI) → Long-running / background tasks
BroadcastReceiver → React to events (boot, battery, network)
WorkManager (modern) → Deferrable background work (next lesson)
Component
Purpose
Has UI
Started Service
Run task until complete
No
Bound Service
Client-server IPC with Activity
No
Foreground Service
Visible ongoing task (music, GPS)
Notification only
BroadcastReceiver
Handle broadcast Intents
No
1Foreground Services
A foreground service performs work the user is actively aware of — music playback, fitness tracking, file download. It must show a persistent notification and is less likely to be killed by the system.
Background services run without user interaction. Modern Android heavily restricts them to save battery — most deferrable work should use WorkManager instead.
Android 8+ restriction: Background services cannot run freely when the app is in the background. Use foreground services for ongoing tasks, WorkManager for deferrable jobs, and coroutines for in-app async work.
Background restrictions summary
Android Version
Restriction
Alternative
8.0 (Oreo)
Background service limits
Foreground service or JobScheduler
9.0 (Pie)
Foreground service requires permission
Declare foregroundServiceType
12+
Cannot start foreground service from background
Exact alarm or user-visible action
14+
Foreground service types enforced
Match type to actual work
When to use what
User-visible ongoing task → Foreground Service
Deferrable background work → WorkManager
Short task while app is open → Coroutine / Thread in Activity
Immediate system event → BroadcastReceiver (registered in code)
Music stops when app killed → Don't use plain background Service
3Started Service
A started service is triggered with startService() or startForegroundService(). It runs until it stops itself or is stopped by the client. It does not return results to the caller directly.
System restarts service after kill; Intent is null
START_NOT_STICKY
System does not restart after kill
START_REDELIVER_INTENT
Restarts with last Intent redelivered
4Bound Service
A bound service offers a client-server interface. Components bind to it with bindService(), call methods through a Binder, and unbind when done. The service is destroyed when no clients are bound.
Local bound service with Binder
Kotlin — TimerService.ktclass TimerService : Service() {
private val binder = TimerBinder()
private var seconds = 0
private var isRunning = false
inner class TimerBinder : Binder() {
fun getService(): TimerService = this@TimerService
}
fun startTimer() {
isRunning = true
// Increment seconds in coroutine / handler
}
fun stopTimer() { isRunning = false }
fun getElapsedSeconds(): Int = seconds
override fun onBind(intent: Intent?): IBinder = binder
}
Bind from Activity with ServiceConnection
Kotlin — MainActivity.ktclass MainActivity : AppCompatActivity() {
private var timerService: TimerService? = null
private var isBound = false
private val connection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
val binder = service as TimerService.TimerBinder
timerService = binder.getService()
isBound = true
}
override fun onServiceDisconnected(name: ComponentName?) {
timerService = null
isBound = false
}
}
override fun onStart() {
super.onStart()
Intent(this, TimerService::class.java).also { intent ->
bindService(intent, connection, Context.BIND_AUTO_CREATE)
}
}
override fun onStop() {
super.onStop()
if (isBound) {
unbindService(connection)
isBound = false
}
}
fun onStartTimerClick() {
timerService?.startTimer()
}
}
Started + Bound combined
Kotlin — Service can be both// Started: startForegroundService() keeps it alive
// Bound: bindService() lets Activity call methods
// Unbind does NOT stop a started service — call stopSelf() explicitly
5BroadcastReceiver Basics
A BroadcastReceiver receives and handles broadcast Intent objects. Use them to respond to system events or send messages between app components.
Two registration types
Type
Registration
Lifetime
Static (Manifest)
Declared in AndroidManifest.xml
Always active (limited actions on Android 8+)
Dynamic (Context)
registerReceiver() in code
Active while registered; must unregister
Dynamic receiver in Activity
Kotlin — Register in Activityclass MainActivity : AppCompatActivity() {
private val batteryReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val level = intent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
val scale = intent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1
val percent = (level * 100 / scale.toFloat()).toInt()
binding.tvBattery.text = "Battery: $percent%"
}
}
override fun onResume() {
super.onResume()
val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
registerReceiver(batteryReceiver, filter)
}
override fun onPause() {
unregisterReceiver(batteryReceiver)
super.onPause()
}
}
Send custom broadcast
Kotlin — App-local broadcast// Send
val intent = Intent("com.example.app.ACTION_TASK_COMPLETE").apply {
putExtra("task_id", 42)
putExtra("result", "success")
}
sendBroadcast(intent)
// Receive (register with same action string)
private val taskReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val taskId = intent?.getIntExtra("task_id", -1)
Toast.makeText(context, "Task $taskId done!", Toast.LENGTH_SHORT).show()
}
}
System broadcasts are sent by Android when device state changes — network connectivity, battery level, screen on/off, boot completed. Most require dynamic registration on Android 8+.
Common system broadcast actions
Action Constant
Triggered When
ACTION_BOOT_COMPLETED
Device finishes booting
ACTION_BATTERY_CHANGED
Battery level or status changes
ACTION_POWER_CONNECTED
Charger plugged in
ACTION_POWER_DISCONNECTED
Charger unplugged
CONNECTIVITY_ACTION
Network connectivity changes
ACTION_SCREEN_ON / OFF
Screen turns on or off
ACTION_AIRPLANE_MODE_CHANGED
Airplane mode toggled
ACTION_LOCALE_CHANGED
System language changed
Network connectivity receiver
Kotlin — Network change listenerclass NetworkReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val cm = context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val network = cm.activeNetwork
val capabilities = cm.getNetworkCapabilities(network)
val isConnected = capabilities?.hasCapability(
NetworkCapabilities.NET_CAPABILITY_INTERNET
) == true
if (isConnected) {
Log.d("Network", "Back online — sync pending data")
} else {
Log.d("Network", "Offline — queue requests")
}
}
}
// Register dynamically
registerReceiver(NetworkReceiver(), IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))
Modern best practice: For network, battery, and connectivity changes, prefer ConnectivityManager.NetworkCallback, WorkManager constraints, and BroadcastReceiver only when necessary. Keep onReceive() short — offload heavy work to a Service or WorkManager.
7Hands-On Exercises
1
Create a started service that simulates a file download with a 5-second delay, logs progress, then calls stopSelf().
2
Convert the download service to a foreground service with a progress notification. Update the notification as download progresses.
3
Build a bound service that tracks elapsed seconds. Bind from an Activity and display the timer in a TextView.
4
Register a dynamic BroadcastReceiver for ACTION_BATTERY_CHANGED and show battery percentage in the UI.
5
Send a custom app broadcast when a background task completes. Update a TextView in another Activity when received.
6
Create a manifest-declared BootReceiver that logs a message on device boot (test on emulator with cold boot).
7
Bonus: Listen for network connectivity changes and show a Snackbar when the device goes offline or comes back online.
Services & BroadcastReceivers MCQ Practice
10 Basic MCQs10 Advanced MCQs
10 Basic Services & BroadcastReceivers MCQs
1
Service runs?
AOnly layouts
BBackground work without UI
COnly notifications UI
DGradle builds
Explanation: Long-running or heavy tasks outside Activity lifecycle.
2
Foreground Service shows?
AOnly toast
BNothing visible
CPersistent notification
DApp drawer icon only
Explanation: Required visible notification so user knows work continues.
3
Started Service begun with?
AsendBroadcast only
BbindService only
CstartActivity
DstartService(intent)
Explanation: Service runs until stopSelf or stopService.
4
Bound Service communicates via?
AIBinder interface
BSharedPreferences only
CLayout XML
DRoom DAO
Explanation: Client binds and calls methods on service binder.
5
BroadcastReceiver receives?
ASQL queries
BSystem or app-wide broadcast Intents
CTouch events only
DGradle sync
Explanation: Responds to events like BOOT_COMPLETED, CONNECTIVITY_CHANGE.
6
IntentService (deprecated) ran work on?
AGPU
BMain thread only
CBackground worker thread
DBinder only
Explanation: Replaced by WorkManager and coroutines for most cases.
7
stopSelf() in Service?
AClears database
BKills entire OS
CUninstalls app
DStops started service if no pending starts
Explanation: Used when background job completes.
8
LocalBroadcastManager?
ADeprecated — use LiveData/Flow/EventBus patterns in-app
BRequired for all apps
CReplaces Firebase
DSystem only
Explanation: For in-app events prefer ViewModel/Flow over implicit local broadcasts.
9
BOOT_COMPLETED receiver needs?
ACamera permission
BRECEIVE_BOOT_COMPLETED permission in manifest
CInternet only
DNo declaration
Explanation: Manifest permission plus intent-filter for ACTION_BOOT_COMPLETED.
10
Service declared in?
AGradle only
Bstrings.xml
CAndroidManifest.xml
Dlayout XML
Explanation: All Service subclasses must be registered.
10 Advanced Services & BroadcastReceivers MCQs
11
Android 8+ background Service limits?
ANo limits
BBackground start restricted — use foreground or WorkManager
CServices banned
DOnly bound allowed always
Explanation: startForegroundService + promote within timeout for visible work.
12
WorkManager vs Service?
AWorkManager replaces all Services always
BIdentical
CWorkManager for deferrable guaranteed work; Service for user-visible immediate
DService for SQL only
Explanation: Choose based on urgency, constraints, and user visibility.
13
Sticky broadcast?
ASame as ordered
BDefault for all apps
CRequired for GPS
DRemoved — security risk
Explanation: Sticky broadcasts deprecated; use other state mechanisms.
14
Ordered broadcast?
AReceivers run sequentially; abortBroadcast stops chain