Skip to the workspace
Pebellslearn by building
Intents and the Other Components  ›  Capstone Project

Services, Broadcast Receivers and Content Providers

Services 16 minBesides activities, Android has three more app components: services for work without a screen, broadcast receivers for reacting to events, and content providers for sharing data.

android / Services, Broadcast Receivers and Content Providers

Explore each scene at your own pace.

01 · The rule

The rule

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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.