Mqtt断开连接disconnect报错原因

在使用Mqtt进行及时通信时,为避免服务关闭后仍占用服务器资源,需在应用退出时调用MqttAndroidClient.disconnect()。然而,直接调用会导致未解注册广播而引发错误。解决办法是在disconnect前添加Thread.sleep(50)延迟,确保广播解注册完成,防止断开连接错误。
摘要由CSDN通过智能技术生成

最近在用mqtt做及时通信,mqtt服务本身是放在本地的service服务里面运行,在应用关闭的时候,mqtt服务也要断开连接,不然会继续占用服务器的cpu,增加服务器负担。所以,在service的onDestroy中,需要调用MqttAndroidClient.disconnect()方法,但是会出现如下报错Log:

Service org.eclipse.paho.android.service.MqttService has leaked IntentReceiver org.eclipse.paho.android.service.AlarmPingSender$AlarmReceiver@29e9a4b8 that was originally registered here. Are you missing a call to unregisterReceiver()?
    android.app.IntentReceiverLeaked: Service org.eclipse.paho.android.service.MqttService has leaked IntentReceiver org.eclipse.paho.android.service.AlarmPingSender$AlarmReceiver@29e9a4b8 that was originally registered here. Are you missing a call to unregisterReceiver()?
        at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:903)
        at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:704)
        at android.app.ContextImpl.registerReceiverInternal(ContextImpl.ja
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值