广播的使用(动态广播)
程序员文章站
2022-03-03 22:02:49
使用场景:收银机会有主副屏,当主屏显示会员时,需要副屏也同时显示,此时可以选择使用广播进行通讯1注册广播(设置过滤标记)IntentFilter myIntentFilter = new IntentFilter(); myIntentFilter.addAction("con.screen.ACTION1"); myIntentFilter.addAction(ModeSelect.ACTION_SCREEN); context.registerR...
使用场景:
收银机会有主副屏,当主屏显示会员时,需要副屏也同时显示,此时可以选择使用广播进行通讯
1注册广播(设置过滤标记)
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("con.screen.ACTION1");
myIntentFilter.addAction(ModeSelect.ACTION_SCREEN);
context.registerReceiver(mBroadcastReceiver, myIntentFilter);
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case "con.screen.ACTION1":
if(intent.getBooleanExtra("isMember",false)){
Log.e("wy","run: "+ "接受了是会员广播");
text_danjia.setText("会员单价");
}
else {
Log.e("wy","run: "+ "接受了不是会员广播");
text_danjia.setText("单价");
}
break;
case ModeSelect.ACTION_SCREEN:
Bundle bundle = intent.getBundleExtra("haveChooseListSer");
LogCatUtil.longLog("ACTION_SCREEN","--------msg---------"+bundle.getSerializable("haveChooseListSer").toString());
updateResult((ArrayList<HashMap<String, String>>)bundle.getSerializable("haveChooseListSer"));
break;
case ModeSelect.ACTION_SCREEN_CLOSE://关闭副屏
dismiss();
break;
case ModeSelect.ACTION_SCREEN_PICTURE://更新广告展示
break;
}
}
};
2发送广播
Intent intent = new Intent();
intent.setAction("con.screen.ACTION1");
intent.putExtra("isMember",isMember);
getActivity().sendBroadcast(intent);
Log.e("wy","run: "+ "发送时会员广播");
3使用bundle发送一个ArrayList集合
private ArrayList<HashMap<String, String>> haveChooseListSer = new ArrayList<>();
Intent intent = new Intent();
intent.setAction(ModeSelect.ACTION_SCREEN);
Bundle bundle = new Bundle();
bundle.putSerializable("haveChooseListSer",haveChooseListSer);
intent.putExtra("haveChooseListSer", bundle);
本文地址:https://blog.csdn.net/xiyangyang8110/article/details/108141198