paho.mqtt.android
                                
                                 paho.mqtt.android copied to clipboard
                                
                                    paho.mqtt.android copied to clipboard
                            
                            
                            
                        Sometime I can't stop service run MqttAndroidClient
Please fill out the form below before submitting, thank you!
- [v] Bug exists Release Version 1.1.1 (Java Repository Master Branch)
Description of Bug:
I use Service run MqttAndroidClient and use flag START_STICKY. When I logout account, i will stop my service. stopService(new Intent(getActivity(), ABCService.class)); But Sometime ABCService can't stop, MqttService just running. I don't know reason.
Console Log output (if available):
Do you have any logs collected when you try to stop the service? It would suggest that something is causing the client to hang and so the service can't stop. Has the connection been closed first?
Please check code MySevice.java
public void onDestroy() { super.onDestroy(); try { if (mqttAndroidClient != null) { mqttAndroidClient.unsubscribe(TOPIC); mqttAndroidClient.disconnect(); } } catch (Exception e) { Log.d(TAG, "! Disconnect failed!"); e.printStackTrace(); } Log.d(TAG, "onDestroy Service!"); } I call function to stop service. stopService(new Intent(getActivity(), MyService.class));_ But sometime MQTT in service can't stop logcat still does not stop working. Sample: Register alarmreceiver to MqttServiceMqttService.pingSender.Android478 D/AlarmPingSender: Unregister alarmreceiver to MqttServiceAndroid478 D/AlarmPingSender: Schedule next alarm at 1503748388874 D/AlarmPingSender: Alarm scheule using setExactAndAllowWhileIdle, next: 60000 D/AlarmPingSender: Schedule next alarm at 1503748328982
Sory i can't use tag  ^^
Stop service and cause leakage
logcat :
2019-06-06 15:20:51.542 18926-18926/com.example.myapplication E/ActivityThread: Service com.example.myapplication.MQttService1 has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@f277926 that was originally bound here
android.app.ServiceConnectionLeaked: Service com.example.myapplication.MQttService1 has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@f277926 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.
I call function to stop service. stopService(new Intent(getActivity(), MQttService1.class));
Please check code MQttService1.java
@Override
public void onDestroy() {
    Log.e(TAG, "onDestroy: "+"servicer" );
    try {
        client.unsubscribe(topicArray);
        client.disconnect();
    } catch (MqttException e) {
        e.printStackTrace();
    }
    //stopSelf();
    super.onDestroy();
}
I found that not waiting for MqttService to disconnect will cause leaks; I'm currently using
public void destroyConnection() {
    try {
        client.unregisterResources();
        client.disconnect(0);
    } catch (MqttException e) {
        e.printStackTrace();
    }
}
By calling client.disconnect(0); you won't leave Paho any wait time, and will therefore disconnect immediatly - or at least that's what it seems to do. This way I have no leaks anymore.
Hi @davidevertuani, I try your solution, but it still run. :(
Schedule next alarm at 1589079675068
Hi, I also ran into that leaked ServiceConnection issue. The following disconnect setup fixed it for me:
mqttClient?.disconnect(null, object : IMqttActionListener {
    override fun onSuccess(asyncActionToken: IMqttToken?) {
        println("Mqtt disconnect onSuccess")
        mqttClient?.unregisterResources()
        mqttClient?.setCallback(null)
        mqttClient?.close()
        mqttClient = null
    }
    override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
        println("Mqtt disconnect onFailure")
    }
})