Service
A Service runs work without a screen. Today most background work uses WorkManager (later lesson). Services remain important for music playback, navigation, and other ongoing tasks the user knows about.
Foreground Service
kotlin
class DownloadService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = NotificationCompat.Builder(this, "downloads")
.setContentTitle("Downloading")
.setSmallIcon(R.drawable.ic_download)
.setOngoing(true)
.build()
startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC) // must show a notification
// do the work in a coroutine, then stopSelf()
return START_NOT_STICKY
}
override fun onBind(intent: Intent?): IBinder? = null
}
xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<service android:name=".DownloadService" android:foregroundServiceType="dataSync" android:exported="false" />
Strict rules: Android limits background services to save battery. Foreground services need a type, a matching permission and a visible notification, and recent Android versions restrict starting them from the background. Prefer WorkManager for deferrable work.
BroadcastReceiver
kotlin
class PowerReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_POWER_CONNECTED) {
Log.d("Power", "Charger connected")
}
}
}
// Register in code (works for most system events, and the receiver ends with the screen)
val receiver = PowerReceiver()
DisposableEffect(Unit) {
ContextCompat.registerReceiver(context, receiver, IntentFilter(Intent.ACTION_POWER_CONNECTED),
ContextCompat.RECEIVER_NOT_EXPORTED)
onDispose { context.unregisterReceiver(receiver) }
}
Most system broadcasts can no longer be declared in the manifest. Register at runtime while your app is running.
ContentProvider
A ContentProvider shares structured data with other apps, such as contacts or media. You will mostly use providers (through ContentResolver), and rarely write one.
kotlin
val cursor = context.contentResolver.query(
ContactsContract.Contacts.CONTENT_URI, arrayOf(ContactsContract.Contacts.DISPLAY_NAME), null, null, null)
cursor?.use { while (it.moveToNext()) Log.d("Contact", it.getString(0)) }
| Component | Modern advice |
| Service | Use for ongoing user-visible tasks. Use WorkManager for deferrable work |
| BroadcastReceiver | Register at runtime. Avoid manifest receivers |
| ContentProvider | Use FileProvider for sharing files. Rarely write your own |
Remember: Always ask: does the user know this is running? If not, it probably belongs in WorkManager.
Conclusion
Services, receivers and providers are less common in new apps, but you will meet them in real code. Respect modern background limits.
Quick Summary: Foreground services need a notification and type, receivers are registered at runtime and WorkManager handles most background tasks.