2

我正在使用Paho Android Service API,我连接onResume和断开连接onPause。在运行时,我可以subscribe从设备published上通过mosquitto Broker. 当我按下他时BackButton,应用程序崩溃并显示以下logcat错误。

请让我知道如何解决这个问题。

注意

1_因为我正在使用Paho Android Service API我将所需的库导入到项目中。
在我的项目清单文件的元素application中,我添加了这一行

2_我没有在我的框架中注册任何接收器,接收器包含在我导入以使用 Paho API 的库中

<!-- Mqtt Service -->
    <service android:name="org.eclipse.paho.android.service.MqttService" >
    </service>

日志猫

     Activity com.example.mqtt_test_00.MessaginActivity   
    has leaked IntentReceiver org.eclipse.paho.android.service.MqttAndroidClient@43075698 that was   
    originally registered here. Are you missing a call to unregisterReceiver()?
 android.app.IntentReceiverLeaked: Activity   
    com.example.mqtt_test_00.MessaginActivity has leaked IntentReceiver  
    org.eclipse.paho.android.service.MqttAndroidClient@43075698 that was originally registered here.  
    Are you missing a call to unregisterReceiver()?
    at android.app.LoadedApk$ReceiverDispatcher.
    <init>(LoadedApk.java:814)
    at 
    android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:610)
    at 
    android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1762)
    at  
    android.app.ContextImpl.registerReceiver(ContextImpl.java:1742)
    at  
    android.app.ContextImpl.registerReceiver(ContextImpl.java:1736)
    at 
    android.content.ContextWrapper.registerReceiver(ContextWrapper.java:478)
    at org
    .eclipse.paho.android.service.MqttAndroidClient.registerReceiver(MqttAndroidClient.java:410)

    at  
    org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:388)
    at   
    com.example.mqtt_test_00.Sub_Frag$5.onClick(Sub_Frag.java:98)
 android.app.ServiceConnectionLeaked: Activity 
    com.example.mqtt_test_00.MessaginActivity has leaked ServiceConnection 
    org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@430756f0 that was 
    originally bound here
    at android.app.LoadedApk$ServiceDispatcher.<init>
    (LoadedApk.java:988)
    at 
    org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:385)
    at   
    com.example.mqtt_test_00.Sub_Frag$5.onClick(Sub_Frag.java:98 )
4

4 回答 4

1

logcat 非常清楚这种情况。看来您没有取消注册接收器。

您应该通过在您的 Activity 拥有的生命周期方法之一(onPause()、onStop())中调用 unregisterReceiver() 来做到这一点。

unregisterReceiver(yourReceiver); 

其中 yourReceiver 很可能是 MqttAndroidClient 的一个实例(每个 logcat 输出)

于 2014-11-21T12:44:03.977 回答
0

我不会重复其他成员的建议和建议,但我唯一想说的是,调用client.disconnect. 你打过电话吗?unregisterservicedisconnect

于 2014-11-21T13:51:08.073 回答
0

你的问题是:

11-21 13:26:51.703: E/ActivityThread(28310): android.app.IntentReceiverLeaked: Activity   
com.example.mqtt_test_00.MessaginActivity has leaked IntentReceiver  
org.eclipse.paho.android.service.MqttAndroidClient@43075698 that was originally      registered here.  
Are you missing a call to unregisterReceiver()?

你没有在同一个班级取消注册服务,所以你遇到了这个问题,试试这样,

@Override
public void onPause() {
    super.onPause();
    try
    {
       unregisterReceiver(this.intentReceiver);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
于 2014-11-21T12:45:26.850 回答
0

MqttAndroidClient 有一个名为 unregisterResources 的方法,如果有的话,它将取消注册接收者。

用法是这样的

MqttConnection 连接 = MqttConnections.getInstance(this).getConnection(clientHandle); connection.getClient().unregisterResources();

于 2016-04-12T06:14:07.093 回答