Android fundamentals 07.3:Broadcast receivers
Tutorial source : Google CodeLab
Date : 2021/04/06
Complete course : 教程目录 (java).
Note : The link in this article requires Google access
1、Welcome
This practical codelab is part of Unit 3: Working in the background in the Android Developer Fundamentals (Version 2) course. You will get the most value out of this course if you work through the codelabs in sequence:
- For the complete list of codelabs in the course, see Codelabs for Android Developer Fundamentals (V2).
- For details about the course, including links to all the concept chapters, apps, and slides, see Android Developer Fundamentals (Version 2).
Note: This course uses the terms “codelab” and “practical” interchangeably.
Introduction
Broadcasts are messages that the Android system and Android apps send when events occur that might affect the functionality of other apps or app components. For example, the Android system sends a system broadcast when the device boots up, or when headphones are connected or disconnected. If the wired headset is unplugged, you might like your media app to pause the music.
Your Android app can also broadcast events, for example when new data is downloaded that might interest some other app. Events that your app delivers are called custom broadcasts.
In general, you can use broadcasts as a messaging system across apps and outside of the normal user flow.
A broadcast is received by any app or app component that has a broadcast receiver registered for that action. BroadcastReceiver
is the base class for code that receives broadcast intents. To learn more about broadcast receivers, see the Broadcasts overview and the Intent
reference.
Note: While the Intent
class is used to send and receive broadcasts, the Intent
broadcast mechanism is completely separate from intents that are used to start activities.
In this practical, you create an app that responds to a change in the charging state of the device. To do this, your app receives and responds to a system broadcast, and it also sends and receives a custom broadcast.
What you should already know
You should be able to:
- Identify key parts of the
AndroidManifest.xml
file. - Create Implicit intents.
What you’ll learn
- How to subclass a
BroadcastReceiver
and implement it. - How to register for system broadcast intents.
- How to create and send custom broadcast intents.
What you’ll do
- Subclass a
BroadcastReceiver
to show a toast when a broadcast is received. - Register your receiver to listen for system broadcasts.
- Send and receive a custom broadcast intent.
2、App overview
The PowerReceiver app will register a BroadcastReceiver
that displays a toast message when the device is connected or disconnected from power. The app will also send and receive a custom broadcast to display a different toast message.
3、Task 1. Set up the PowerReceiver project
1.1 Create the project
- In Android Studio, create a new Java project called PowerReceiver. Accept the default options and use the Empty Activity template.
- To create a new broadcast receiver, select the package name in the Android Project View and navigate to File > New > Other > Broadcast Receiver.
- Name the class CustomReceiver. Make sure that Java is selected as the source language, and that Exported and Enabled are selected. Exported allows your broadcast receiver to receive broadcasts from outside your app. Enabled allows the system to instantiate the receiver.
1.2 Register your receiver for system broadcasts
A system broadcast is a message that the Android system sends when a system event occurs. Each system broadcast is wrapped in an Intent
object:
- The intent’s action field contains event details such as
android.intent.action.HEADSET_PLUG
, which is sent when a wired headset is connected or disconnected. - The intent can contain other data about the event in its extra field, for example a
boolean
extra indicating whether a headset is connected or disconnected.
Apps can register to receive specific broadcasts. When the system sends a broadcast, it routes the