Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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?

Sorting Sprout's three featuresStep 1 of 4
  1. Audio guide

    Doing work the user notices without a screen → foreground service.

Drills

Answer fast — trust the rule.

quick drill

Which component should react when the phone finishes booting?

quick drill

What must a foreground service show while it runs?

quick drill

Sprout reads the user's contacts through URIs that start with content://. What component supplies that data?

quick drill

Sprout must sync plant data once a night, even if the app is closed. What is the recommended modern tool?

quick drill

How many kinds of app components does Android define?

Remember

  • Service works, receiver listens, provider shares.
  • Sort features by: doing, hearing, or sharing?
  • Keep onReceive short; use foreground services only for user-visible work and WorkManager for deferrable jobs.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPick the component

For each Sprout feature, name the app component that fits and explain why: (1) reschedule watering reminders after the phone restarts, (2) let the user pick a plant photo from the device's gallery, (3) play a long plant-care audio guide that keeps going while the user uses other apps.