What are `Broadcast Receivers` in Android, and how would you use them?

Instruction: Explain the concept of Broadcast Receivers and provide an example scenario where using a Broadcast Receiver is appropriate.

Context: This question checks the candidate's understanding of Broadcast Receivers and their ability to implement them to respond to system-wide broadcast announcements or custom broadcasts within their application.

Updated

Official answer

A BroadcastReceiver handles eligible Intent-based announcements from the system or another app. Receivers can be declared in the manifest or registered with a Context; allowed registration and delivery depend on the action, OS/target version and app state.

For example, a manifest receiver for BOOT_COMPLETED can restore an app's own scheduled reminders after reboot when the required permission and platform conditions are met. Its onReceive should validate the action and perform only brief work or schedule appropriate durable work. Do not assume boot permits any foreground service to start or that delivery is an exact-time guarantee.

onReceive normally runs on the main thread. Starting an arbitrary background thread and returning is unsafe for work that must finish: the process can lose importance after the receiver returns. goAsync permits short asynchronous handling with finish called reliably, but does not provide an unlimited execution window.

A context-registered receiver must be scoped and unregistered appropriately. Choose exported/not-exported behavior deliberately; protect sensitive broadcasts with appropriate permissions and validate incoming extras. Registration is not authorization.

There is no general LOCATION_CHANGED system broadcast to subscribe to. Location updates use location APIs and their callbacks or PendingIntent delivery, subject to consent, permissions and background limits. Test denied permissions, absent delivery and cancellation rather than claiming automatic battery savings.

Broadcasts

Related Questions