AndroidX使用Paho MQTT报找不到android/support/v4/content/LocalBroadcastManager

网上有直接引用support-v4包的,但我用的AndroidX,不能为这个类再引用support-v4
直接自己创建这个类,把androidx.localbroadcastmanager.content.LocalBroadcastManager改改就行。
虽然奇葩但能解决问题

package android.support.v4.content;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;import androidx.annotation.NonNull;import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;/*** Helper to register for and send broadcasts of Intents to local objects* within your process.  This has a number of advantages over sending* global broadcasts with {@link android.content.Context#sendBroadcast}:* <ul>* <li> You know that the data you are broadcasting won't leave your app, so* don't need to worry about leaking private data.* <li> It is not possible for other applications to send these broadcasts to* your app, so you don't need to worry about having security holes they can* exploit.* <li> It is more efficient than sending a global broadcast through the* system.* </ul>*/
public final class LocalBroadcastManager {private static final class ReceiverRecord {final IntentFilter filter;final BroadcastReceiver receiver;boolean broadcasting;boolean dead;ReceiverRecord(IntentFilter _filter, BroadcastReceiver _receiver) {filter = _filter;receiver = _receiver;}@Overridepublic String toString() {StringBuilder builder = new StringBuilder(128);builder.append("Receiver{");builder.append(receiver);builder.append(" filter=");builder.append(filter);if (dead) {builder.append(" DEAD");}builder.append("}");return builder.toString();}}private static final class BroadcastRecord {final Intent intent;final ArrayList<ReceiverRecord> receivers;BroadcastRecord(Intent _intent, ArrayList<ReceiverRecord> _receivers) {intent = _intent;receivers = _receivers;}}private static final String TAG = "LocalBroadcastManager";private static final boolean DEBUG = false;private final Context mAppContext;private final HashMap<BroadcastReceiver, ArrayList<ReceiverRecord>> mReceivers= new HashMap<>();private final HashMap<String, ArrayList<ReceiverRecord>> mActions = new HashMap<>();private final ArrayList<BroadcastRecord> mPendingBroadcasts = new ArrayList<>();static final int MSG_EXEC_PENDING_BROADCASTS = 1;private final Handler mHandler;private static final Object mLock = new Object();private static android.support.v4.content.LocalBroadcastManager mInstance;@NonNullpublic static android.support.v4.content.LocalBroadcastManager getInstance(@NonNull Context context) {synchronized (mLock) {if (mInstance == null) {mInstance = new android.support.v4.content.LocalBroadcastManager(context.getApplicationContext());}return mInstance;}}private LocalBroadcastManager(Context context) {mAppContext = context;mHandler = new Handler(context.getMainLooper()) {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case MSG_EXEC_PENDING_BROADCASTS:executePendingBroadcasts();break;default:super.handleMessage(msg);}}};}/*** Register a receive for any local broadcasts that match the given IntentFilter.** @param receiver The BroadcastReceiver to handle the broadcast.* @param filter Selects the Intent broadcasts to be received.** @see #unregisterReceiver*/public void registerReceiver(@NonNull BroadcastReceiver receiver,@NonNull IntentFilter filter) {synchronized (mReceivers) {ReceiverRecord entry = new ReceiverRecord(filter, receiver);ArrayList<ReceiverRecord> filters = mReceivers.get(receiver);if (filters == null) {filters = new ArrayList<>(1);mReceivers.put(receiver, filters);}filters.add(entry);for (int i=0; i<filter.countActions(); i++) {String action = filter.getAction(i);ArrayList<ReceiverRecord> entries = mActions.get(action);if (entries == null) {entries = new ArrayList<ReceiverRecord>(1);mActions.put(action, entries);}entries.add(entry);}}}/*** Unregister a previously registered BroadcastReceiver.  <em>All</em>* filters that have been registered for this BroadcastReceiver will be* removed.** @param receiver The BroadcastReceiver to unregister.** @see #registerReceiver*/public void unregisterReceiver(@NonNull BroadcastReceiver receiver) {synchronized (mReceivers) {final ArrayList<ReceiverRecord> filters = mReceivers.remove(receiver);if (filters == null) {return;}for (int i=filters.size()-1; i>=0; i--) {final ReceiverRecord filter = filters.get(i);filter.dead = true;for (int j=0; j<filter.filter.countActions(); j++) {final String action = filter.filter.getAction(j);final ArrayList<ReceiverRecord> receivers = mActions.get(action);if (receivers != null) {for (int k=receivers.size()-1; k>=0; k--) {final ReceiverRecord rec = receivers.get(k);if (rec.receiver == receiver) {rec.dead = true;receivers.remove(k);}}if (receivers.size() <= 0) {mActions.remove(action);}}}}}}/*** Broadcast the given intent to all interested BroadcastReceivers.  This* call is asynchronous; it returns immediately, and you will continue* executing while the receivers are run.** @param intent The Intent to broadcast; all receivers matching this*     Intent will receive the broadcast.** @see #registerReceiver** @return Returns true if the intent has been scheduled for delivery to one or more* broadcast receivers.  (Note tha delivery may not ultimately take place if one of those* receivers is unregistered before it is dispatched.)*/public boolean sendBroadcast(@NonNull Intent intent) {synchronized (mReceivers) {final String action = intent.getAction();final String type = intent.resolveTypeIfNeeded(mAppContext.getContentResolver());final Uri data = intent.getData();final String scheme = intent.getScheme();final Set<String> categories = intent.getCategories();final boolean debug = DEBUG ||((intent.getFlags() & Intent.FLAG_DEBUG_LOG_RESOLUTION) != 0);if (debug) Log.v(TAG, "Resolving type " + type + " scheme " + scheme+ " of intent " + intent);ArrayList<ReceiverRecord> entries = mActions.get(intent.getAction());if (entries != null) {if (debug) Log.v(TAG, "Action list: " + entries);ArrayList<ReceiverRecord> receivers = null;for (int i=0; i<entries.size(); i++) {ReceiverRecord receiver = entries.get(i);if (debug) Log.v(TAG, "Matching against filter " + receiver.filter);if (receiver.broadcasting) {if (debug) {Log.v(TAG, "  Filter's target already added");}continue;}int match = receiver.filter.match(action, type, scheme, data,categories, "LocalBroadcastManager");if (match >= 0) {if (debug) Log.v(TAG, "  Filter matched!  match=0x" +Integer.toHexString(match));if (receivers == null) {receivers = new ArrayList<ReceiverRecord>();}receivers.add(receiver);receiver.broadcasting = true;} else {if (debug) {String reason;switch (match) {case IntentFilter.NO_MATCH_ACTION: reason = "action"; break;case IntentFilter.NO_MATCH_CATEGORY: reason = "category"; break;case IntentFilter.NO_MATCH_DATA: reason = "data"; break;case IntentFilter.NO_MATCH_TYPE: reason = "type"; break;default: reason = "unknown reason"; break;}Log.v(TAG, "  Filter did not match: " + reason);}}}if (receivers != null) {for (int i=0; i<receivers.size(); i++) {receivers.get(i).broadcasting = false;}mPendingBroadcasts.add(new BroadcastRecord(intent, receivers));if (!mHandler.hasMessages(MSG_EXEC_PENDING_BROADCASTS)) {mHandler.sendEmptyMessage(MSG_EXEC_PENDING_BROADCASTS);}return true;}}}return false;}/*** Like {@link #sendBroadcast(Intent)}, but if there are any receivers for* the Intent this function will block and immediately dispatch them before* returning.*/public void sendBroadcastSync(@NonNull Intent intent) {if (sendBroadcast(intent)) {executePendingBroadcasts();}}@SuppressWarnings("WeakerAccess") /* synthetic access */void executePendingBroadcasts() {while (true) {final BroadcastRecord[] brs;synchronized (mReceivers) {final int N = mPendingBroadcasts.size();if (N <= 0) {return;}brs = new BroadcastRecord[N];mPendingBroadcasts.toArray(brs);mPendingBroadcasts.clear();}for (int i=0; i<brs.length; i++) {final BroadcastRecord br = brs[i];final int nbr = br.receivers.size();for (int j=0; j<nbr; j++) {final ReceiverRecord rec = br.receivers.get(j);if (!rec.dead) {rec.receiver.onReceive(mAppContext, br.intent);}}}}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/112095.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[人工智能-综述-12]:第九届全球软件大会(南京)有感 -1-程序员通过大模型增强自身软件研发效率的同时,也在砸自己的饭碗

目录 前言&#xff1a; 一、什么是软件工程 1.1 什么软件工程 1.2 影响软件开发效能的三大因素 1.3 AI大模型是如何提升软件工程全过程效率的 二、AI大模型如何提升软件项目管理效率 2.1 概述 2.2 案例或工具 三、AI大模型如何提升软件开发工具的效率 3.1 概述 3.2 …

JAVA 如何 改变 System.out.println(““); 的流向。

平日我们的内容都是输出在控制&#xff0c;现在小加变动&#xff0c;我们可以让他输出到文件。 package cn.cqvie.chapter01.exam1;import java.io.FileNotFoundException; import java.io.PrintStream;public class RediretOutputStream {public static void main(String[] ar…

Git GUI使用笔记

看这个视频 Git GUI基本使用_哔哩哔哩_bilibili 1 下载 Git-2.42.0.2-64Window64位安装包-最新版资源-CSDN文库 安装软件就一路next就可以 2 配置 空白处右键&#xff0c;选择Open Git Bash here &#xff0c;输入下面两行配置信息 git config --global user.name "Y…

大语言模型面试心路历程【0 offer版】

记录自己大语言模型面试的经历&#xff0c;旨在可以帮助更多想要从事此方面的小伙伴。 北银金科 1.InstructGPT的训练过程 2.critic网络的作用 3.LSTM的原理&#xff0c;GRU与LSTM有什么不同 4.讲一下Bert的结构 5.讲一下自己的论文【KBQA相关】 6.GLM的结构和微调了哪些参数 …

小程序之自定义组件 结合案例(会议OA的会议/投票管理及个人中心的搭建)详解 (4)

⭐⭐ 小程序专栏&#xff1a;小程序开发专栏 ⭐⭐ 个人主页&#xff1a;个人主页 目录 一.前言 二.小程序自定义组件及其使用 2.1 自定义组件的使用 三.使用自定义组件完成会议功能界面的实现 3.1 导航栏的实现 3.2 会议界面内容的实现 四.投票管理界面 五.个人中心 今天…

uview组件使用笔记

图标样式 修改图标的样式 通过color参数修改图标的颜色通过size参数修改图标的大小&#xff0c;单位为rpx 效果图 <u-icon name"photo" color"#2979ff" size"28"></u-icon>图片图标 1.3.0 这里说的图片图标&#xff0c;指的是小…

7、Linux驱动开发:设备-自动创建设备节点

目录 &#x1f345;点击这里查看所有博文 随着自己工作的进行&#xff0c;接触到的技术栈也越来越多。给我一个很直观的感受就是&#xff0c;某一项技术/经验在刚开始接触的时候都记得很清楚。往往过了几个月都会忘记的差不多了&#xff0c;只有经常会用到的东西才有可能真正记…

【力扣刷题】回文链表、环形链表、合并两个有序链表

&#x1f40c;个人主页&#xff1a; &#x1f40c; 叶落闲庭 &#x1f4a8;我的专栏&#xff1a;&#x1f4a8; c语言 数据结构 javaEE 操作系统 Redis 石可破也&#xff0c;而不可夺坚&#xff1b;丹可磨也&#xff0c;而不可夺赤。 刷题篇 一、回文链表1.1 题目描述1.2 思路分…

msvcr120.dll缺失怎么修复,快速修复msvcr120.dll丢失的三个有效方法

随着计算机技术的不断发展&#xff0c;我们在使用软件或游戏时经常会遇到各种错误提示&#xff0c;其中找不到msvcr120.dll就是一种常见的错误。那么&#xff0c;msvcr120.dll是什么&#xff1f;它的作用是什么&#xff1f;如何修复这一错误呢&#xff1f;本文将为您详细介绍几…

7.7亿参数,超越5400亿PaLM!UW谷歌提出「分步蒸馏」,只需80%训练数据|ACL 2023

LLM不实用&#xff0c;小模型蒸馏才是「现实」的大模型应用路线&#xff0c;全面领先微调技术&#xff01;土豪请无视。。。 大型语言模型虽然性能优异&#xff0c;可以用零样本或少样本提示解决新任务&#xff0c;但LLM在实际应用部署时却很不实用&#xff0c;内存利用效率低…

基于Java的汽车维修预约管理系统设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09; 代码参考数据库参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计划导师、全栈领域优质创作者&am…

HZOJ-270:最大子序和

题目描述 ​ 输入一个长度为 n&#xfffd; 的整数序列&#xff0c;从中找出一段不超过 M&#xfffd; 的连续子序列&#xff0c;使得整个序列的和最大。 ​ 例如 1&#xff0c;−3,5,1&#xff0c;−2,31&#xff0c;−3,5,1&#xff0c;−2,3&#xff1a; ​ 当 m4&#x…

群晖synology DSM 7.2设置钉钉Webhooks通知

现在越来越多的小伙伴都有了自己的Nas系统&#xff0c;为了更加方便的接收Nas的消息&#xff0c;这篇文章带着大家一起配置一个钉钉&#xff08;机器人&#xff09;即时消息通知 首先登录钉钉的开放平台&#xff1a;开发者后台统一登录 - 钉钉统一身份认证 1.创建一个机器人&…

关于spring的xml文件中的xmlns,xsi,schemaLocation

关于spring xml文件中的xmlns,xsi:schemaLocation 首先我们看到的一个spring的配置文件大概如下面这个样子&#xff1a; <?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans" //这表…

基于蛇优化优化的BP神经网络(分类应用) - 附代码

基于蛇优化优化的BP神经网络&#xff08;分类应用&#xff09; - 附代码 文章目录 基于蛇优化优化的BP神经网络&#xff08;分类应用&#xff09; - 附代码1.鸢尾花iris数据介绍2.数据集整理3.蛇优化优化BP神经网络3.1 BP神经网络参数设置3.2 蛇优化算法应用 4.测试结果&#x…

源码包安装zabbix

下载zabbix server源码包 wget -O zabbix-4.0.4.tar.gz https://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/4.0.4/zabbix-4.0.4.tar.gz/download安装依赖 yum install wget telnet net-tools python-paramiko gcc gcc-c dejavu-sans-fonts python-setu…

基于Java的勤工助学管理系统设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09; 代码参考数据库参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计划导师、全栈领域优质创作者&am…

CEC2013(MATLAB):白鲨优化算法(White Shark Optimizer,WSO)求解CEC2013(提供MATLAB代码及参考文献)

一、白鲨优化算法原理 白鲨优化算法&#xff08;White Shark Optimizer&#xff0c;WSO&#xff09;由Malik Braik等人于2022年提出&#xff0c;该算法受大白鲨导航和觅食时具有的非凡听觉和嗅觉启发。该算法思路新颖&#xff0c;策略高效。【精选】单目标应用&#xff1a;白鲨…

微信小程序修改van-popup的背景颜色

效果图&#xff1a; van-popup背景颜色渐变 使用深度修改样式不生效&#xff0c;直接在 custom-style里面修改即可&#xff1b; <van-popup position"bottom"custom-style"height:25%;background:linear-gradient(95deg, #F8FCFF -0.03%, #EDF5FF 64.44…

01_introduction_to_diffusers_CN

&#x1f917; Diffusers 介绍 来源&#xff1a;https://github.com/huggingface/diffusion-models-class/blob/main/unit1/01_introduction_to_diffusers.ipynb 预备知识 在进入 Notebook 之前&#xff0c;你需要: &#x1f4d6; 阅读第一单元的材料&#x1f917; 在 Hugg…