android广播发送拦截接收完整代码(包括所有文件名)
时间: 2023-08-04 11:15:42 浏览: 66
好的,以下是一个完整的 Android 广播拦截器示例代码,包括所有文件名:
1. 创建一个 BroadcastInterceptor 类,实现 BroadcastReceiver 接口:
BroadcastInterceptor.java
```java
public class BroadcastInterceptor extends BroadcastReceiver {
private static final String TAG = "BroadcastInterceptor";
private BroadcastReceiver mReceiver;
public BroadcastInterceptor(BroadcastReceiver receiver) {
mReceiver = receiver;
}
@Override
public void onReceive(Context context, Intent intent) {
// 拦截广播
Log.d(TAG, "Intercepted broadcast: " + intent.getAction());
// 在这里进行自定义逻辑处理,例如取消广播、修改广播内容等
// 转发广播给原始接收器
mReceiver.onReceive(context, intent);
}
}
```
2. 在你的项目中创建一个 BroadcastUtils 工具类,用于发送广播:
BroadcastUtils.java
```java
public class BroadcastUtils {
public static final String ACTION_MY_BROADCAST = "com.example.MY_BROADCAST";
public static void sendMyBroadcast(Context context, String data) {
Intent intent = new Intent(ACTION_MY_BROADCAST);
intent.putExtra("data", data);
context.sendBroadcast(intent);
}
}
```
3. 在你的 MainActivity 中注册广播接收器:
MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private BroadcastReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 注册广播接收器
mReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter(BroadcastUtils.ACTION_MY_BROADCAST);
registerReceiver(mReceiver, filter);
}
@Override
protected void onDestroy() {
// 取消注册广播接收器
unregisterReceiver(mReceiver);
super.onDestroy();
}
private class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 处理接收到的广播
String data = intent.getStringExtra("data");
Log.d("MainActivity", "Received broadcast data: " + data);
}
}
}
```
4. 在需要拦截广播的地方,创建 BroadcastInterceptor 对象,并将它注册为广播接收器:
```java
BroadcastInterceptor interceptor = new BroadcastInterceptor(new MainActivity.MyBroadcastReceiver());
IntentFilter filter = new IntentFilter(BroadcastUtils.ACTION_MY_BROADCAST);
registerReceiver(interceptor, filter);
```
这样就可以拦截应用程序中发送的指定广播,并在拦截器中进行自定义逻辑处理。需要注意的是,拦截广播需要在应用程序的清单文件中为每个接收器定义一个优先级,以便系统可以按照正确的顺序发送广播。
相关推荐
![](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)