android通过广播或事件总线等机制通知所有 Activity 重新加载资源文件
时间: 2023-12-16 09:02:28 浏览: 44
可以通过发送自定义广播或使用事件总线等方式来通知所有 Activity 重新加载资源文件。具体实现步骤如下:
1. 定义一个自定义广播,例如:
```
public static final String ACTION_REFRESH_RESOURCE = "com.example.ACTION_REFRESH_RESOURCE";
```
2. 在需要重新加载资源文件的地方发送广播,例如:
```
Intent intent = new Intent(ACTION_REFRESH_RESOURCE);
sendBroadcast(intent);
```
3. 在所有需要重新加载资源的 Activity 中注册广播接收器,在接收到广播时重新加载资源,例如:
```
private BroadcastReceiver mRefreshReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// 重新加载资源
}
};
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(ACTION_REFRESH_RESOURCE);
registerReceiver(mRefreshReceiver, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mRefreshReceiver);
}
```
这样,当发送广播时,所有注册了该广播接收器的 Activity 都会收到通知,并重新加载资源文件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)