码迷,mamicode.com
首页 > 移动开发 > 详细

Android BroadcastReceiver传值

时间:2020-07-21 22:28:19      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:receive   text   cas   min   广播接收器   dac   override   equal   android   

1、定义两个常量:广播名和广播键值

    public static final  String ACTION_BONED = "ACTION_BONED";

    public static final String ACTION_BONED_DATA = "ACTION_BONED_DATA";

2、在Activity或Service中发送广播 

  Intent mIntent = new Intent(BluetoothBonedReceiver.ACTION_BONED);
  mIntent.putExtra(BluetoothBonedReceiver.ACTION_BONED_DATA, sb.toString());
  sendBroadcast(mIntent);

3、定义广播接收器

 private BroadcastReceiver bonedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if(BluetoothBonedReceiver.ACTION_BONED.equals(action)){
                String data = intent.getStringExtra(BluetoothBonedReceiver.ACTION_BONED_DATA);
                Message msg = new Message();
                msg.obj = data;
                handler.sendMessage(msg);
            }
        }
    };

4、注册广播

   IntentFilter intentFilter = new IntentFilter();
   // 监视蓝牙关闭和打开的状态
   intentFilter1.addAction(BluetoothBonedReceiver.ACTION_BONED);
   registerReceiver(bonedReceiver, intentFilter);

5、注销广播

 unregisterReceiver(bonedReceiver);

 

Android BroadcastReceiver传值

标签:receive   text   cas   min   广播接收器   dac   override   equal   android   

原文地址:https://www.cnblogs.com/zoro-zero/p/13356764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!