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.
The shortcut
Ask one question of any non-screen feature: is it doing, hearing, or sharing?
Audio guide
Doing work the user notices without a screen → foreground service.
Drills
Answer fast — trust the rule.
Which component should react when the phone finishes booting?
What must a foreground service show while it runs?
Sprout reads the user's contacts through URIs that start with content://. What component supplies that data?
Sprout must sync plant data once a night, even if the app is closed. What is the recommended modern tool?
How many kinds of app components does Android define?
Remember
- Service works, receiver listens, provider shares.
- Sort features by: doing, hearing, or sharing?
- Keep
onReceiveshort; use foreground services only for user-visible work and WorkManager for deferrable jobs.
