声明广播
private BroadcastReceiver broadcastReceiver;1.发送广播 Intent intent = new Intent("com.example.MY_ACTION"); intent.putExtra("message", "6"); getContext().sendBroadcast(intent);
2.注册接收广播
// 初始化 BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {// 处理接收到的广播消息String message = intent.getStringExtra("message");if (message.equals("6")) {//处理相应的逻辑// 取消注册广播接收器context.unregisterReceiver(broadcastReceiver);}} }; // 注册广播接收器 IntentFilter intentFilter = new IntentFilter("com.example.MY_ACTION"); mContext.registerReceiver(broadcastReceiver, intentFilter);