声明广播 private BroadcastReceiver broadcastReceiver; 1.发送广播 Intent intent = new Intent("com.example.MY_ACTION"); intent.putExtra("message", "6"); getContext().sendBroadcast(intent); 2.注册接收广播 // 初始化 BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public 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); |
原文地址:https://blog.csdn.net/xige1995/article/details/131658152
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:https://www.msipo.com/article-358.html 如若内容造成侵权/违法违规/事实不符,请联系MSIPO邮箱:3448751423@qq.com进行投诉反馈,一经查实,立即删除!
Copyright © 2024, msipo.com