android的广播发送与接收
2016-07-21 16:35
417 查看
一、发送广播
<span style="white-space:pre"> </span>/** * 发送后厨状态广播 * @param context 上下文 * @param status自定义的状态码 */ public static void sendKitchenPrintStatusBroadcast(Context context,int status) { Intent intent = new Intent(); intent.setAction(OneLeadPosConstants.SYNCHRONIZE_KITCHEN_PRINT_STATUS_ACTION);//设置action intent.putExtra(OneLeadPosConstants.SYNCHRONIZE_KITCHEN_PRINT_STATUS_ACTION, status);//放入传输数据 context.sendBroadcast(intent);//有序广播发送 }
二、广播接收者
一定要记得在初始化方法中注册registerBoradcastReceiver();,在onDestroy方法中解除注册this.unregisterReceiver(mBroadcastReceiver);<span style="white-space:pre"> </span>/** * 注册后厨状态广播 * */ private void registerBoradcastReceiver(){ IntentFilter myIntentFilter = new IntentFilter();//创建意图过滤器 myIntentFilter.addAction(OneLeadPosConstants.SYNCHRONIZE_KITCHEN_PRINT_STATUS_ACTION);//设置过滤器action //注册广播 广播接收者,广播过滤器 registerReceiver(mBroadcastReceiver, myIntentFilter); }
<span style="white-space:pre"> </span> /** * 后厨状态广播接收者 * */ private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals(OneLeadPosConstants.SYNCHRONIZE_KITCHEN_PRINT_STATUS_ACTION)){ int status=intent.getIntExtra(OneLeadPosConstants.SYNCHRONIZE_KITCHEN_PRINT_STATUS_ACTION, 80); if(status==80){//后厨打印机没有启用 Log.d("hainan", "hainan======后厨打印机没有启用"); }else if (status==50) {//连接不上后厨打印机 Log.d("hainan", "hainan======连接不上后厨打印机"); }else if (status==100) {//连接成功 Log.d("hainan", "hainan======连接成功"); } } } };
相关文章推荐
- 利用IE收听“广播”
- Android广播接收机制详细介绍(附短信接收实现)
- Android中BroadcastReceiver(异步接收广播Intent)的使用
- Jquery实现仿腾讯微博发表广播
- Android使用广播(BroadCast)实现强制下线的方法
- Android基于广播事件机制实现简单定时提醒功能代码
- Android使用BroadcastReceiver实现手机开机之后显示画面的功能
- Android提高之BroadcastReceiver实例详解
- 详解Android中BroadCastReceiver组件
- Android编程四大组件之BroadcastReceiver(广播接收者)用法实例
- Android实现静态广播监听器的方法
- Android中的广播、服务、数据库、通知、包等术语的原理和介绍(图解)
- Android中的广播(BroadCast)详细介绍
- Android中的广播和广播接收器代码实例
- Android开发之BroadcastReceiver用法实例分析
- Android基础知识之broadcast广播详解
- Android开发之广播机制浅析
- Android 广播大全 Intent Action 事件详解
- 广播