在安卓api33中,如何使用BroadcastReceiver
时间: 2024-05-03 08:22:23 浏览: 20
在安卓API 33中,使用BroadcastReceiver需要以下步骤:
1. 创建BroadcastReceiver类并继承BroadcastReceiver,实现onReceive方法。
```java
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: 处理接收到的广播
}
}
```
2. 在AndroidManifest.xml文件中注册BroadcastReceiver。
```xml
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.MY_BROADCAST" />
</intent-filter>
</receiver>
```
3. 发送广播。
```java
Intent intent = new Intent("android.intent.action.MY_BROADCAST");
sendBroadcast(intent);
```
注意事项:
- 广播的IntentFilter必须与BroadcastReceiver中注册的一致。
- 如果广播需要传递数据,可以使用Intent的putExtra方法添加数据,接收时使用getXXXExtra方法获取数据。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)