CodeLab:Android fundamentals 07.3:Broadcast receivers

这个教程介绍了如何在Android中创建和使用广播接收器。你将学习如何为系统广播注册接收器,发送和接收自定义广播。内容包括创建一个响应设备充电状态变化的PowerReceiver应用,动态注册和注销广播接收器,以及处理系统和自定义广播。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

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

  1. In Android Studio, create a new Java project called PowerReceiver. Accept the default options and use the Empty Activity template.
  2. To create a new broadcast receiver, select the package name in the Android Project View and navigate to File > New > Other > Broadcast Receiver.
  3. 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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值