Services, Broadcast Receivers and Content Providers
Sprout needs three things that are not screens: reminders that survive a phone restart, access to photos in the gallery, and a plant-care audio guide that keeps playing after the user leaves the app. Activities cannot do these. Android has three more components for exactly these jobs.
The rule
Service works, receiver listens, provider shares.
Android has four app components, all declared in the manifest:
| Component | Job | Sprout example |
|---|---|---|
| Activity | A screen the user interacts with | The plant list |
| Service | Keeps work going without a screen | The audio guide |
| Broadcast receiver | Reacts to an event announced to the system | Phone finished restarting |
| Content provider | Shares structured data through content:// URIs |
The gallery's photos |
Service. A service runs code without UI. Work the user can notice, such as playing audio, should be a foreground service, which must show a notification. For deferrable background jobs like "sync tonight", modern apps use WorkManager instead of a hand-written service, because the system limits background services.
Broadcast receiver. The system and apps announce events as broadcasts: boot finished, battery low, airplane mode changed. A receiver's onReceive must finish quickly; hand longer work to WorkManager.
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) scheduleReminders(context)
}
}Content provider. A provider exposes data to other apps through URIs, like a small, permission-controlled database API. You rarely write one; you use them through a ContentResolver, for example to read contacts or media.