Android Studio 单刷《第一行代码》系列 07 —— Broadcast 广播
时间: 2023-08-05 17:11:26 浏览: 72
Broadcast 广播是 Android 系统中的一种重要的组件通信方式,它可以让应用程序之间进行消息传递,从而实现应用程序之间的数据交换和功能协同。广播可以被用来处理一些系统事件或应用程序内部的特定事件。在本篇文章中,我们将学习如何在 Android 应用程序中使用广播。
一、广播的基本概念
广播是指一种可以跨应用程序发送和接收消息的机制,它允许应用程序向全局范围内的其他应用程序通知某些事件的发生。Android 中的广播可以分为两类:
1.标准广播(Normal Broadcast):这种广播是完全异步的,所有的接收者都会在同一时刻接收到广播消息,因此它们之间没有任何优先级的区别。使用标准广播时,所有接收者都无法终止广播的传播,这也是标准广播的一个缺点。
2.有序广播(Ordered Broadcast):这种广播是同步执行的,所有的接收者都是按照优先级顺序依次接收广播消息的。在广播传递过程中,每个接收者都可以截断广播的传播,使得后面的接收者无法收到广播消息。如果某个接收者截断了广播的传播,那么其后面的接收者就无法收到广播消息。这种广播的优先级由高到低依次为:1000、500、0、-500、-1000。
二、发送和接收广播
1.发送广播
在 Android 应用程序中,我们可以通过以下代码来发送广播:
```java
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendBroadcast(intent);
```
上述代码中,我们首先创建了一个 Intent 对象,然后将其 action 设置为 "com.example.broadcasttest.MY_BROADCAST",这个 action 可以自己定义。最后,我们调用了 sendBroadcast() 方法来发送广播。
2.接收广播
要接收广播,需要在代码中注册一个 BroadcastReceiver 对象,并在其 onReceive() 方法中处理广播消息。以下是一个简单的例子:
```java
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
}
}
```
在上述代码中,我们创建了一个 MyBroadcastReceiver 类,并实现了其 onReceive() 方法。当收到广播消息时,系统会自动调用该方法,并将广播消息以 Intent 对象的形式传递给该方法。在 onReceive() 方法中,我们可以根据 Intent 对象中携带的信息来进行相应的处理,例如弹出一个 Toast 提示框。
3.注册广播接收器
在 Android 应用程序中,我们需要使用 IntentFilter 对象来指定要接收的广播类型。以下是一个注册广播接收器的例子:
```java
MyBroadcastReceiver receiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.broadcasttest.MY_BROADCAST");
registerReceiver(receiver, intentFilter);
```
在上述代码中,我们首先创建了一个 MyBroadcastReceiver 对象,然后创建了一个 IntentFilter 对象,并将要接收的广播类型设置为 "com.example.broadcasttest.MY_BROADCAST"。最后,我们调用 registerReceiver() 方法来注册广播接收器。
4.注销广播接收器
当我们不再需要接收某个广播时,应该及时将其注册的广播接收器进行注销。以下是一个注销广播接收器的例子:
```java
unregisterReceiver(receiver);
```
在上述代码中,我们调用 unregisterReceiver() 方法来注销广播接收器。
三、有序广播的使用
有序广播可以让我们按照优先级顺序接收广播消息,并且可以截断广播的传播。以下是一个有序广播的例子:
```java
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
sendOrderedBroadcast(intent, null);
```
在上述代码中,我们调用了 sendOrderedBroadcast() 方法来发送有序广播。由于没有指定接收者的权限,因此我们将其设置为 null。
接下来,我们需要在代码中注册一个 BroadcastReceiver 对象,并在其 onReceive() 方法中处理广播消息。以下是一个简单的例子:
```java
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String msg = getResultData();
Toast.makeText(context, msg + " Received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
setResultData("Hello MainActivity");
}
}
```
在上述代码中,我们首先调用 getResultData() 方法来获取上一个接收者设置的数据,然后弹出一个 Toast 提示框,并将自己的数据设置为 "Hello MainActivity"。
接下来,我们需要指定广播接收者的优先级。在 AndroidManifest.xml 文件中,我们可以使用 priority 属性来设置广播接收者的优先级。以下是一个简单的例子:
```xml
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
<priority android:priority="1000" />
</receiver>
```
在上述代码中,我们将 MyBroadcastReceiver 类注册为广播接收者,并将其优先级设置为 1000。
为了演示有序广播的效果,我们可以再注册一个 BroadcastReceiver 对象,并将其优先级设置为 500。以下是一个简单的例子:
```java
public class AnotherBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String msg = getResultData();
Toast.makeText(context, msg + " Received in AnotherBroadcastReceiver", Toast.LENGTH_SHORT).show();
setResultData("Hello MainActivity");
}
}
```
在上述代码中,我们将 AnotherBroadcastReceiver 类注册为广播接收者,并将其优先级设置为 500。
最后,我们在 AndroidManifest.xml 文件中注册这两个广播接收者,代码如下:
```xml
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
<priority android:priority="1000" />
</receiver>
<receiver android:name=".AnotherBroadcastReceiver">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
<priority android:priority="500" />
</receiver>
```
在上述代码中,我们将 MyBroadcastReceiver 和 AnotherBroadcastReceiver 类都注册为广播接收者,并指定了它们的优先级。
在执行上述代码后,我们可以看到,在发送广播时,首先会将广播消息发送给优先级为 1000 的 MyBroadcastReceiver 类,然后再发送给优先级为 500 的 AnotherBroadcastReceiver 类。在 MyBroadcastReceiver 类的 onReceive() 方法中,我们可以获取到 AnotherBroadcastReceiver 类设置的数据,然后弹出一个 Toast 提示框,并将自己的数据设置为 "Hello MainActivity"。最后,我们可以看到,弹出的 Toast 提示框中显示的内容为 "Hello MainActivity Received in MyBroadcastReceiver",这表明 MyBroadcastReceiver 类成功地截断了广播的传播。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)