壮心未与年俱老,死去犹能作鬼雄。这篇文章主要讲述Android BroadcastReceiver传值相关的知识,希望能为你提供帮助。
1、定义两个常量:广播名和广播键值
public static finalString ACTION_BONED = "ACTION_BONED"; public static final String ACTION_BONED_DATA = "https://www.songbingjia.com/android/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 = https://www.songbingjia.com/android/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传值】
推荐阅读
- spring IOC - AnnotationConfigApplicationContext#this
- Android Service作为后台一直运行监测案例
- uniapp实现锚点跳转
- uni-app阻止事件向父级冒泡
- Flask学习第6天(app.add_url_rule调用路由)
- DAY22 - Happy Vacation
- Android(Sqlite3数据库查看方法)
- Android Studio(Kotlin)之RecyclerView
- ipad与windows互传文件(不需要安装app)