很多时候我们有这样的需求,比如说,订单支付成功,需要更新订单列表或订单详情的订单状态,这时候我们就可以用到广播.

首先我们要使用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

最新文章

  1. 求一个区间[a,b]中数字1出现的次数
  2. 7 -- Spring的基本用法 -- 7...
  3. Hibernate学习0.Hibernate入门
  4. 手机通过WIFI连上ZXV10 H618B路由器但不能上网问题的解决
  5. on the wane
  6. 基于Visual C++2013拆解世界五百强面试题--题6-double类型逆序
  7. sp1是什么意思
  8. ORA-00600[17059]错误
  9. 《.NET 设计规范》第 9 章:常用的设计模式
  10. android webview重定向 返回按钮死循环问题修改
  11. [Linux]安装pyenv
  12. iOS UITextfield只允许输入数字和字母,长度限制
  13. SQL Server远程连接 provider: Named Pipes Provider, error: 40 解决方法
  14. UVALive - 6434 (思维题)
  15. HDFS集群PB级数据迁移方案-DistCp生产环境实操篇
  16. 注解工具ButterKnife用法和注意点
  17. Java-Swing常用布局管理器
  18. sencha touch调试时Please close other application using ADB: Monitor, DDMS, Eclipse
  19. 安装gensim报错:Original error was: DLL load failed: 找不到指定的模块。 Command &quot;python setup.py egg_info&quot; failed with error code 1 in C:\Users\xubing\AppData\Local\Temp\pip-install-nta89iep\gensim\
  20. tomcat部署war包启动后请求无响应,一直报404

热门文章

  1. fread()创建文件和file_exists()文件缓存问题
  2. Python爬虫系列-BeautifulSoup详解
  3. Lecture 3
  4. python GIL锁、进程池与线程池、同步异步
  5. perl-basic-数据类型&amp;引用
  6. python基础学习笔记——字典
  7. jnative 使用
  8. Android工具 Hierarchy Viewer 分析
  9. JavaScript日期时间格式化函数
  10. “玲珑杯”线上赛 Round #17 河南专场