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);}}}}}
}
相关文章:
AndroidX使用Paho MQTT报找不到android/support/v4/content/LocalBroadcastManager
网上有直接引用support-v4包的,但我用的AndroidX,不能为这个类再引用support-v4 直接自己创建这个类,把androidx.localbroadcastmanager.content.LocalBroadcastManager改改就行。 虽然奇葩但能解决问题 package android.support.v4.content…...
Filter与Listener(过滤器与监听器)
1.Filter 1.过滤器概述 过滤器——Filter,它是JavaWeb三大组件之一。另外两个是Servlet和Listener 它可以对web应用中的所有资源进行拦截,并且在拦截之后进行一些特殊的操作 在程序中访问服务器资源时,当一个请求到来,服务器首…...
【刷题篇】反转链表
文章目录 一、206.反转链表二、92.反转链表 ||三、25. K 个一组翻转链表 一、206.反转链表 class Solution { public://使用头插//三个指针也可以ListNode* reverseList(ListNode* head) {if(headnullptr)return nullptr;ListNode* curhead;ListNode* newheadnew ListNode(0);L…...
【C语言小游戏--猜数字】
文章目录 前言1.游戏描述2.代码实现2.1打印菜单2.2构建基础框架2.3玩游戏2.3.1生成随机数2.3.1.1rand()2.3.1.2srand()2.3.1.3time() 2.3.2game() 2.4自己设定猜的次数 3.完整代码 前言 猜数字小游戏是我们大多数人学习C语言时都会了解到的一个有趣的C语言小游戏,下…...
Vue计算属性computed和监听watch
1.计算属性 初衷:为了解决模块里面有太多的计算逻辑让模版难以维护。 计算属性可以依赖一个数据也可以依赖多个数据的变化 使用场景: 商品单价和数量改变时,商品总价更改。如果写在方法里面,那么每次修改商品单价和数量时都会…...
HTTP介绍 原理 消息结构 客户端请求 服务器响应 HTTP状态码
一、HTTP介绍二、HTTP工作原理HTTP三点注意事项 三、HTTP消息结构四、客户端请求消息五、服务器响应消息HTTP请求方法 七、HTTP响应头信息八、HTTP状态码(HTTP Status Code)下面是常见的HTTP状态码:HTTP状态码分类HTTP状态码列表 一、HTTP介绍…...
linux性能分析(五)如何学习linux性能优化
一 如何学习linux性能优化 强调: 由于知识记忆曲线以及某些知识点不常用,所以一定要注重复习思考: 如何进行能力转义以及能力嫁接? --> 真正站在巨人的肩膀上性能调优的目的: 不影响系统稳定性的资源最大利用化补充: 性能…...
1402. 做菜顺序 --力扣 --JAVA
题目 一个厨师收集了他 n 道菜的满意程度 satisfaction ,这个厨师做出每道菜的时间都是 1 单位时间。 一道菜的 「 like-time 系数 」定义为烹饪这道菜结束的时间(包含之前每道菜所花费的时间)乘以这道菜的满意程度,也就是 time[i…...
Springboot踩坑-request body重复读问题
背景 在一次业务开发中,由于需要在拦截器中对一个http请求中request body内容做解析和判断,所以用了httpServletRequest的getInputStream解析了request body内容,之后导致了拦截器处理成功后,原来的业务接口处报request body not…...
C++ 类和对象(六)赋值运算符重载
1 运算符重载 C为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数, 也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。 函数名字为:关键字operator后面接需…...
rust学习-trait std::cmp::PartialEq、Eq、PartialOrd、Ord
PartialEq 介绍 pub trait PartialEq<Rhs = Self> whereRhs: ?Sized, {// Required method,后文有讲解这个注释fn eq(&self, other: &Rhs) -> bool;// Provided method,后文有讲解这个注释fn ne(&self, other: &Rhs) -> bool {... } }x.eq(y)…...
k8s pod根据指标自动扩缩容举例
目录 基于 内存 指标实现pod自动扩缩容 举例配置 基于 cpu 指标实现pod自动扩缩容 举例配置 基于请求数(次/秒) 指标实现pod自动扩缩容 举例配置 基于 http请求响应时间 (ms) 指标实现pod自动扩缩容 举例配置 基于 Java GC暂停时间 (ms) 指标实现…...
深、浅拷贝之间的关系
深、浅拷贝之间的关系 什么是赋值 赋值是将某一数值或对象赋给某个变量的过程,分为下面 2 部分 基本数据类型:赋值,赋值之后两个变量互不影响引用数据类型:赋址,两个变量具有相同的引用,指向同一个对象&…...
服务器数据恢复-某银行服务器硬盘数据恢复案例
服务器故障&分析: 某银行的某一业务模块崩溃,无法正常使用。排查服务器故障,发现运行该业务模块的服务器中多块硬盘离线,导致上层应用崩溃。 故障服务器内多块硬盘掉线,硬盘掉线数量超过服务器raid阵列冗余级别所允…...
仪器器材经营小程序商城的作用是什么
互联网发展下,数字化转型已经成为常态,仅依赖传统线下经营模式将很难再增长。 作为产品销售及客户维护度高的仪器器材行业,拥有自营商城平台是必要的,不仅可以解决以上难题,还利于打造自身品牌多渠道传播,…...
京东数据分析:2023年9月京东洗烘套装品牌销量排行榜!
鲸参谋监测的京东平台9月份洗烘套装市场销售数据已出炉! 根据鲸参谋平台的数据显示,今年9月份,京东平台洗烘套装的销量为7100,环比下降约37%,同比增长约87%;销售额为6000万,环比下降约48%&#…...
论文阅读-多目标强化学习-envelope MOQ-learning
introduction 一种多目标强化学习算法,来自2019 Nips《A Generalized Algorithm for Multi-Objective Reinforcement Learning and Policy Adaptation》本文引用代码全部来源于论文中的链接。主要参考run_e3c_double.py文件 1 总体思想 1.将输入中加入多目标的偏…...
【原创】【考法总结】指针*与++结合的题目考法总结
代码均已调试出结果,放心食用,大致总共5种考法 【理论铺垫】①a[i]恒等价于(ai)即*(&a[0]i);i类似偏移量(别忘a代表数组首元素地址即&a[0]) ②*(&a[i])恒等价于a[i]:&a[i]表示a[i]的地址&a…...
react dispatch不生效的坑
一、前言 最近写react antd项目,在A页面中使用了dispatch方法,然后B页面中嵌套A页面,没有问题; 但是在C页面中嵌套A页面的时候,就发现dispatch方法没有执行,也不报错,就很奇怪; 还…...
Mingw快捷安装教程 并完美解决出现的下载错误:The file has been downloaded incorrectly
安装c语言编译器的时候,老是出现The file has been downloaded incorrectly,真的让人 直接去官网拿压缩包:https://sourceforge.net/projects/mingw-w64/files/ (往下拉找到那个x86_64-win32-seh的链接,点击后会自动…...
网络六边形受到攻击
大家读完觉得有帮助记得关注和点赞!!! 抽象 现代智能交通系统 (ITS) 的一个关键要求是能够以安全、可靠和匿名的方式从互联车辆和移动设备收集地理参考数据。Nexagon 协议建立在 IETF 定位器/ID 分离协议 (…...
多云管理“拦路虎”:深入解析网络互联、身份同步与成本可视化的技术复杂度
一、引言:多云环境的技术复杂性本质 企业采用多云策略已从技术选型升维至生存刚需。当业务系统分散部署在多个云平台时,基础设施的技术债呈现指数级积累。网络连接、身份认证、成本管理这三大核心挑战相互嵌套:跨云网络构建数据…...
手游刚开服就被攻击怎么办?如何防御DDoS?
开服初期是手游最脆弱的阶段,极易成为DDoS攻击的目标。一旦遭遇攻击,可能导致服务器瘫痪、玩家流失,甚至造成巨大经济损失。本文为开发者提供一套简洁有效的应急与防御方案,帮助快速应对并构建长期防护体系。 一、遭遇攻击的紧急应…...
盘古信息PCB行业解决方案:以全域场景重构,激活智造新未来
一、破局:PCB行业的时代之问 在数字经济蓬勃发展的浪潮中,PCB(印制电路板)作为 “电子产品之母”,其重要性愈发凸显。随着 5G、人工智能等新兴技术的加速渗透,PCB行业面临着前所未有的挑战与机遇。产品迭代…...
在鸿蒙HarmonyOS 5中实现抖音风格的点赞功能
下面我将详细介绍如何使用HarmonyOS SDK在HarmonyOS 5中实现类似抖音的点赞功能,包括动画效果、数据同步和交互优化。 1. 基础点赞功能实现 1.1 创建数据模型 // VideoModel.ets export class VideoModel {id: string "";title: string ""…...
Vue3 + Element Plus + TypeScript中el-transfer穿梭框组件使用详解及示例
使用详解 Element Plus 的 el-transfer 组件是一个强大的穿梭框组件,常用于在两个集合之间进行数据转移,如权限分配、数据选择等场景。下面我将详细介绍其用法并提供一个完整示例。 核心特性与用法 基本属性 v-model:绑定右侧列表的值&…...
【大模型RAG】Docker 一键部署 Milvus 完整攻略
本文概要 Milvus 2.5 Stand-alone 版可通过 Docker 在几分钟内完成安装;只需暴露 19530(gRPC)与 9091(HTTP/WebUI)两个端口,即可让本地电脑通过 PyMilvus 或浏览器访问远程 Linux 服务器上的 Milvus。下面…...
Caliper 配置文件解析:config.yaml
Caliper 是一个区块链性能基准测试工具,用于评估不同区块链平台的性能。下面我将详细解释你提供的 fisco-bcos.json 文件结构,并说明它与 config.yaml 文件的关系。 fisco-bcos.json 文件解析 这个文件是针对 FISCO-BCOS 区块链网络的 Caliper 配置文件,主要包含以下几个部…...
自然语言处理——循环神经网络
自然语言处理——循环神经网络 循环神经网络应用到基于机器学习的自然语言处理任务序列到类别同步的序列到序列模式异步的序列到序列模式 参数学习和长程依赖问题基于门控的循环神经网络门控循环单元(GRU)长短期记忆神经网络(LSTM)…...
C++课设:简易日历程序(支持传统节假日 + 二十四节气 + 个人纪念日管理)
名人说:路漫漫其修远兮,吾将上下而求索。—— 屈原《离骚》 创作者:Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder😊) 专栏介绍:《编程项目实战》 目录 一、为什么要开发一个日历程序?1. 深入理解时间算法2. 练习面向对象设计3. 学习数据结构应用二、核心算法深度解析…...
