当前位置: 首页 > news >正文

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包的&#xff0c;但我用的AndroidX&#xff0c;不能为这个类再引用support-v4 直接自己创建这个类&#xff0c;把androidx.localbroadcastmanager.content.LocalBroadcastManager改改就行。 虽然奇葩但能解决问题 package android.support.v4.content…...

Filter与Listener(过滤器与监听器)

1.Filter 1.过滤器概述 过滤器——Filter&#xff0c;它是JavaWeb三大组件之一。另外两个是Servlet和Listener 它可以对web应用中的所有资源进行拦截&#xff0c;并且在拦截之后进行一些特殊的操作 在程序中访问服务器资源时&#xff0c;当一个请求到来&#xff0c;服务器首…...

【刷题篇】反转链表

文章目录 一、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语言小游戏&#xff0c;下…...

Vue计算属性computed和监听watch

1.计算属性 初衷&#xff1a;为了解决模块里面有太多的计算逻辑让模版难以维护。 计算属性可以依赖一个数据也可以依赖多个数据的变化 使用场景&#xff1a; 商品单价和数量改变时&#xff0c;商品总价更改。如果写在方法里面&#xff0c;那么每次修改商品单价和数量时都会…...

HTTP介绍 原理 消息结构 客户端请求 服务器响应 HTTP状态码

一、HTTP介绍二、HTTP工作原理HTTP三点注意事项 三、HTTP消息结构四、客户端请求消息五、服务器响应消息HTTP请求方法 七、HTTP响应头信息八、HTTP状态码&#xff08;HTTP Status Code&#xff09;下面是常见的HTTP状态码&#xff1a;HTTP状态码分类HTTP状态码列表 一、HTTP介绍…...

linux性能分析(五)如何学习linux性能优化

一 如何学习linux性能优化 强调&#xff1a; 由于知识记忆曲线以及某些知识点不常用,所以一定要注重复习思考&#xff1a; 如何进行能力转义以及能力嫁接? --> 真正站在巨人的肩膀上性能调优的目的&#xff1a; 不影响系统稳定性的资源最大利用化补充&#xff1a; 性能…...

1402. 做菜顺序 --力扣 --JAVA

题目 一个厨师收集了他 n 道菜的满意程度 satisfaction &#xff0c;这个厨师做出每道菜的时间都是 1 单位时间。 一道菜的 「 like-time 系数 」定义为烹饪这道菜结束的时间&#xff08;包含之前每道菜所花费的时间&#xff09;乘以这道菜的满意程度&#xff0c;也就是 time[i…...

Springboot踩坑-request body重复读问题

背景 在一次业务开发中&#xff0c;由于需要在拦截器中对一个http请求中request body内容做解析和判断&#xff0c;所以用了httpServletRequest的getInputStream解析了request body内容&#xff0c;之后导致了拦截器处理成功后&#xff0c;原来的业务接口处报request body not…...

C++ 类和对象(六)赋值运算符重载

1 运算符重载 C为了增强代码的可读性引入了运算符重载&#xff0c;运算符重载是具有特殊函数名的函数&#xff0c; 也具有其返回值类型&#xff0c;函数名字以及参数列表&#xff0c;其返回值类型与参数列表与普通的函数类似。 函数名字为&#xff1a;关键字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自动扩缩容 举例配置 基于请求数&#xff08;次/秒&#xff09; 指标实现pod自动扩缩容 举例配置 基于 http请求响应时间 (ms) 指标实现pod自动扩缩容 举例配置 基于 Java GC暂停时间 (ms) 指标实现…...

深、浅拷贝之间的关系

深、浅拷贝之间的关系 什么是赋值 赋值是将某一数值或对象赋给某个变量的过程&#xff0c;分为下面 2 部分 基本数据类型&#xff1a;赋值&#xff0c;赋值之后两个变量互不影响引用数据类型&#xff1a;赋址&#xff0c;两个变量具有相同的引用&#xff0c;指向同一个对象&…...

服务器数据恢复-某银行服务器硬盘数据恢复案例

服务器故障&分析&#xff1a; 某银行的某一业务模块崩溃&#xff0c;无法正常使用。排查服务器故障&#xff0c;发现运行该业务模块的服务器中多块硬盘离线&#xff0c;导致上层应用崩溃。 故障服务器内多块硬盘掉线&#xff0c;硬盘掉线数量超过服务器raid阵列冗余级别所允…...

仪器器材经营小程序商城的作用是什么

互联网发展下&#xff0c;数字化转型已经成为常态&#xff0c;仅依赖传统线下经营模式将很难再增长。 作为产品销售及客户维护度高的仪器器材行业&#xff0c;拥有自营商城平台是必要的&#xff0c;不仅可以解决以上难题&#xff0c;还利于打造自身品牌多渠道传播&#xff0c;…...

京东数据分析:2023年9月京东洗烘套装品牌销量排行榜!

鲸参谋监测的京东平台9月份洗烘套装市场销售数据已出炉&#xff01; 根据鲸参谋平台的数据显示&#xff0c;今年9月份&#xff0c;京东平台洗烘套装的销量为7100&#xff0c;环比下降约37%&#xff0c;同比增长约87%&#xff1b;销售额为6000万&#xff0c;环比下降约48%&#…...

论文阅读-多目标强化学习-envelope MOQ-learning

introduction 一种多目标强化学习算法&#xff0c;来自2019 Nips《A Generalized Algorithm for Multi-Objective Reinforcement Learning and Policy Adaptation》本文引用代码全部来源于论文中的链接。主要参考run_e3c_double.py文件 1 总体思想 1.将输入中加入多目标的偏…...

【原创】【考法总结】指针*与++结合的题目考法总结

代码均已调试出结果&#xff0c;放心食用&#xff0c;大致总共5种考法 【理论铺垫】①a[i]恒等价于(ai)即*(&a[0]i)&#xff1b;i类似偏移量&#xff08;别忘a代表数组首元素地址即&a[0]&#xff09; ②*(&a[i])恒等价于a[i]&#xff1a;&a[i]表示a[i]的地址&a…...

react dispatch不生效的坑

一、前言 最近写react antd项目&#xff0c;在A页面中使用了dispatch方法&#xff0c;然后B页面中嵌套A页面&#xff0c;没有问题&#xff1b; 但是在C页面中嵌套A页面的时候&#xff0c;就发现dispatch方法没有执行&#xff0c;也不报错&#xff0c;就很奇怪&#xff1b; 还…...

Mingw快捷安装教程 并完美解决出现的下载错误:The file has been downloaded incorrectly

安装c语言编译器的时候&#xff0c;老是出现The file has been downloaded incorrectly&#xff0c;真的让人 直接去官网拿压缩包&#xff1a;https://sourceforge.net/projects/mingw-w64/files/ &#xff08;往下拉找到那个x86_64-win32-seh的链接&#xff0c;点击后会自动…...

浏览器访问 AWS ECS 上部署的 Docker 容器(监听 80 端口)

✅ 一、ECS 服务配置 Dockerfile 确保监听 80 端口 EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]或 EXPOSE 80 CMD ["python3", "-m", "http.server", "80"]任务定义&#xff08;Task Definition&…...

深入理解JavaScript设计模式之单例模式

目录 什么是单例模式为什么需要单例模式常见应用场景包括 单例模式实现透明单例模式实现不透明单例模式用代理实现单例模式javaScript中的单例模式使用命名空间使用闭包封装私有变量 惰性单例通用的惰性单例 结语 什么是单例模式 单例模式&#xff08;Singleton Pattern&#…...

ffmpeg(四):滤镜命令

FFmpeg 的滤镜命令是用于音视频处理中的强大工具&#xff0c;可以完成剪裁、缩放、加水印、调色、合成、旋转、模糊、叠加字幕等复杂的操作。其核心语法格式一般如下&#xff1a; ffmpeg -i input.mp4 -vf "滤镜参数" output.mp4或者带音频滤镜&#xff1a; ffmpeg…...

【python异步多线程】异步多线程爬虫代码示例

claude生成的python多线程、异步代码示例&#xff0c;模拟20个网页的爬取&#xff0c;每个网页假设要0.5-2秒完成。 代码 Python多线程爬虫教程 核心概念 多线程&#xff1a;允许程序同时执行多个任务&#xff0c;提高IO密集型任务&#xff08;如网络请求&#xff09;的效率…...

python执行测试用例,allure报乱码且未成功生成报告

allure执行测试用例时显示乱码&#xff1a;‘allure’ &#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;ڲ&#xfffd;&#xfffd;&#xfffd;&#xfffd;ⲿ&#xfffd;&#xfffd;&#xfffd;Ҳ&#xfffd;&#xfffd;&#xfffd;ǿ&#xfffd;&am…...

HarmonyOS运动开发:如何用mpchart绘制运动配速图表

##鸿蒙核心技术##运动开发##Sensor Service Kit&#xff08;传感器服务&#xff09;# 前言 在运动类应用中&#xff0c;运动数据的可视化是提升用户体验的重要环节。通过直观的图表展示运动过程中的关键数据&#xff0c;如配速、距离、卡路里消耗等&#xff0c;用户可以更清晰…...

虚拟电厂发展三大趋势:市场化、技术主导、车网互联

市场化&#xff1a;从政策驱动到多元盈利 政策全面赋能 2025年4月&#xff0c;国家发改委、能源局发布《关于加快推进虚拟电厂发展的指导意见》&#xff0c;首次明确虚拟电厂为“独立市场主体”&#xff0c;提出硬性目标&#xff1a;2027年全国调节能力≥2000万千瓦&#xff0…...

提升移动端网页调试效率:WebDebugX 与常见工具组合实践

在日常移动端开发中&#xff0c;网页调试始终是一个高频但又极具挑战的环节。尤其在面对 iOS 与 Android 的混合技术栈、各种设备差异化行为时&#xff0c;开发者迫切需要一套高效、可靠且跨平台的调试方案。过去&#xff0c;我们或多或少使用过 Chrome DevTools、Remote Debug…...

Ubuntu系统复制(U盘-电脑硬盘)

所需环境 电脑自带硬盘&#xff1a;1块 (1T) U盘1&#xff1a;Ubuntu系统引导盘&#xff08;用于“U盘2”复制到“电脑自带硬盘”&#xff09; U盘2&#xff1a;Ubuntu系统盘&#xff08;1T&#xff0c;用于被复制&#xff09; &#xff01;&#xff01;&#xff01;建议“电脑…...

协议转换利器,profinet转ethercat网关的两大派系,各有千秋

随着工业以太网的发展&#xff0c;其高效、便捷、协议开放、易于冗余等诸多优点&#xff0c;被越来越多的工业现场所采用。西门子SIMATIC S7-1200/1500系列PLC集成有Profinet接口&#xff0c;具有实时性、开放性&#xff0c;使用TCP/IP和IT标准&#xff0c;符合基于工业以太网的…...