广播broadcast的使用
2024-09-04 08:20:56
很多时候我们有这样的需求,比如说,订单支付成功,需要更新订单列表或订单详情的订单状态,这时候我们就可以用到广播.
首先我们要使用Intent来发送一个广播
定义一个全局的广播名字
public static final String DOLOGOUTTRUE ="com.example.se7en.dreamcity.do_logout_true";
Intent intent = new Intent();
intent.setAction(DOLOGOUTTRUE);
intent.putExtra("dolog_true", "dolog_true");
sendBroadcast(intent);
注册方式:
1、创建一个继承与BroadcastReceiver的类
实现继承的方法
在使用回调函数来实现 你想要发送的参数,然后在activity判断这个字符串是不是发送的这个,如果是,进行更新。
public class DologoutReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String isTrue = intent.getStringExtra("dolog_true");
if (onDologoutTrue != null) {
onDologoutTrue.getIstrue(isTrue);
} } public interface OnDologoutTrue {
void getIstrue(String state);
} public OnDologoutTrue onDologoutTrue; public void setOnDologoutTrue(OnDologoutTrue onDologoutTrue) {
this.onDologoutTrue = onDologoutTrue;
}
}
在AndroidManifest中注册这个广播
切记 action里的name 和你定义的全局变量DOLOGOUTTRUE的内容一样
<receiver android:name=".receiver.DologoutReceiver">
<intent-filter>
<action android:name="com.example.se7en.dreamcity.do_logout_true"></action> </intent-filter>
</receiver>
在需要更新的页面 初始化,方法扔oncrete里面就可以。
/**
* 广播接收者
*/
private void initReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(DOLOGOUTTRUE);
DologoutReceiver receiver = new DologoutReceiver();
registerReceiver(receiver, intentFilter);
receiver.setOnDologoutTrue(this);
}
最后一步,在你的activity里implements 你的回调函数,完成未实现的方法。
public class MainActivity extends AppCompatActivity implements View.OnClickListener, DologoutReceiver.OnDologoutTrue
@Override
public void getIstrue(String state) {
if (state.equals("dolog_true")) {
loginDialog.show();
}
}
2、代码动态注册广播
orderReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(StaticInApp.UPDATE_ORDER_DETAIL_NEW)) {
getOrderInfo();
}
}
}; IntentFilter filter_update = new IntentFilter();
filter_update.addAction(StaticInApp.UPDATE_ORDER_DETAIL_NEW);
registerReceiver(orderReceiver, filter_update);
切记onDestroy里解注册广播
@Override
protected void onDestroy() {
super.onDestroy();
if(orderReceiver!=null){
unregisterReceiver(orderReceiver);
}
}
遇到的坑:
父类里面定义的广播,子类就算重写receiver,也不会接收到。最好的方法是在各自的子类定义各自的receiver和对应的action。
By LiYing
最新文章
- 求一个区间[a,b]中数字1出现的次数
- 7 -- Spring的基本用法 -- 7...
- Hibernate学习0.Hibernate入门
- 手机通过WIFI连上ZXV10 H618B路由器但不能上网问题的解决
- on the wane
- 基于Visual C++2013拆解世界五百强面试题--题6-double类型逆序
- sp1是什么意思
- ORA-00600[17059]错误
- 《.NET 设计规范》第 9 章:常用的设计模式
- android webview重定向 返回按钮死循环问题修改
- [Linux]安装pyenv
- iOS UITextfield只允许输入数字和字母,长度限制
- SQL Server远程连接 provider: Named Pipes Provider, error: 40 解决方法
- UVALive - 6434 (思维题)
- HDFS集群PB级数据迁移方案-DistCp生产环境实操篇
- 注解工具ButterKnife用法和注意点
- Java-Swing常用布局管理器
- sencha touch调试时Please close other application using ADB: Monitor, DDMS, Eclipse
- 安装gensim报错:Original error was: DLL load failed: 找不到指定的模块。 Command ";python setup.py egg_info"; failed with error code 1 in C:\Users\xubing\AppData\Local\Temp\pip-install-nta89iep\gensim\
- tomcat部署war包启动后请求无响应,一直报404