按下后退按钮时应用程序崩溃

时间:2014-11-21 12:39:15

标签: android service mqtt paho

我正在使用Paho Android Service API,我在onResume中加入,并在onPause中断开连接。在运行时,我可以subscribe从设备published通过mosquitto Broker查看来自PC的BackButton消息。当我按下logcat应用程序时,会显示以下Paho Android Service API错误。

请告诉我如何解决这个问题。

注意

1_since我正在使用application我将所需的库导入到项目中。和
  在我的项目的清单文件的<!-- Mqtt Service --> <service android:name="org.eclipse.paho.android.service.MqttService" > </service> 元素内,我添加了这一行

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

     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 )

logcat的

{{1}}

4 个答案:

答案 0 :(得分:1)

logcat非常清楚这种情况。好像你没有取消注册接收器。

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

unregisterReceiver(yourReceiver); 

WhereReceiver最有可能是MqttAndroidClient的实例(每个logcat输出)

答案 1 :(得分: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();
    }
}

答案 2 :(得分:0)

我不会重复其他成员建议和推荐的内容,但我唯一想说的是,client.disconnect unregister service disconnect。你打电话给{{1}}吗?

答案 3 :(得分:0)

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

用法是这样的

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