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

Android MQTT关于断开连接disconnect报错原因

最近项目遇到一个需求,就是在登录状态的时候。才能接收到消息。所有我在上线,下线状态的时候。做了MQTT断开和连接的动作。然后就是发生了。我们标题的这关键点了。直接报错了。报错的内容如下:

MqttAndroidClient unregisterRecevicer after disconnect throw missing call unregisterRecevicer...

直接报错了。跟剧情完全不一样啊,但是好奇怪。之前是页面销毁就不会报这个错误。之前是退出登录的时候。页面顺带一起销毁Service的。就没问题。然后现在是页面还在。但是给执行Service的绑定和解绑动作的时候就会出现问题。
如果你依赖的也是下面的这个,那么这个帖子应该有希望会帮到你:
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
/*** 开启service*/public static void startService(Context context) {mqttSetUp = null;TOPIC = UUID_PREFIX + SharedCacheUtils.getInstance(context).getUUid();if (context != null) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {context.startForegroundService(new Intent(context.getApplicationContext(), MQTTService.class));} else {context.startService(new Intent(context.getApplicationContext(), MQTTService.class));}}}
​
/*** 关闭service*/public static void stopService(Context context) {if (context != null) {context.stopService(new Intent(context.getApplicationContext(), MQTTService.class));} else {// 如果异常关闭,导致UI关闭,服务没解绑。走elsetry {if (client != null) {client.unregisterResources();client.close();client.disconnect(0);client.setCallback(null);}client = null;} catch (Exception e) {e.printStackTrace();}}}​
这个是我之前的代码,然后我就去查找这个问题的解决思路,然后就发现了很多帖子的作者也遇到了这个问题。报错信息是一样的。但是好像没有解决我的问题。离谱的是,我搜到了好多VIP的帖子。我看不了。哈哈哈。没有分享精神啊!差评。
然后我就去该依赖的报错反馈信息里面找找看。果然有,但是很多都是建议。能不能用还得靠自己实践了才知道有没有帮助。我发现很多帖子都有点像是AI的,很不靠谱。后面通过自己实践之后就解决了,解决方案如下:

/*** 关闭service*/public static void stopService(Context context) {if (context != null) {context.stopService(new Intent(context.getApplicationContext(), MQTTService.class));} else {// 如果异常关闭,导致UI关闭,服务没解绑。走elsetry {if (client != null) {client.unregisterResources();client.close();//client.disconnect(0);client.setCallback(null);}client = null;} catch (Exception e) {e.printStackTrace();}}}
注释disconnect的动作。就不会报错了。如果你有更好的解决方式,欢迎评论哦!下面就是解决方法的出处。希望能帮助到你。


https://github.com/eclipse-paho/paho.mqtt.android/issues/212

http://www.lryc.cn/news/511477.html

相关文章:

  • Unity3D中Huatuo可行性的思维实验详解
  • ES-聚合分析
  • 【CSS in Depth 2 精译_093】16.2:CSS 变换在动效中的应用(上)—— 图标的放大和过渡效果的设置
  • Linux Debian安装ClamAV和命令行扫描病毒方法,以及用Linux Shell编写了一个批量扫描病毒的脚本
  • Spring创建异步线程,使用@Async注解时不指定value可以吗?
  • 二分和离散化
  • 深度学习实战102-基于深度学习的网络入侵检测系统,利用各种AI模型和pytorch框架实现网络入侵检测
  • vue3使用element-plus,解决 el-table 多选框,选中后翻页再回来选中失效问题
  • 网络的类型
  • 实现类似gpt 打字效果
  • 项目需求分析流程
  • idea连接SQL Server数据库_idea连接sqlserver数据库
  • Scala_【2】变量和数据类型
  • u3d中JSON数据处理
  • idea 安装插件(在线安装、离线安装)
  • springboot maven 构建 建议使用 --release 21 而不是 -source 21 -target 21,因为它会自动设置系统模块的位置
  • 离散数学 复习 详细(子群,元素的周期,循环群,合同)
  • Java后端常见问题 (一)jar:unknown was not found in alimaven
  • overleaf中文生僻字显示不正确,显示双线F
  • C语言中的贪心算法
  • 虚幻引擎结构之UWorld
  • 太通透了,Android 流程分析 蓝牙enable流程(stack/hidl)
  • 2.微服务灰度发布落地实践(agent实现)
  • 搭建医疗客服知识库:智慧医疗的基石
  • CES Asia 2025的低空经济展区有哪些亮点?
  • Java/Spring项目包名为何以“com”开头?
  • 影刀进阶应用 | 知乎发布想法
  • v-if 和 v-for 优先级
  • 【数据结构与算法】单向链表
  • 网络编程UDP—socket实现(C++)