Closing app when trying to connect to an MQTT server but the server is not running

Viewed 104

Good afternoon, I created a service to listen to an MQTT Broker server, however when I start the app and the server is not active it is closing the application, when I activate the MQTT server and open the app it works perfectly. The code block that connects to the server is already implementing a try / catch, but even so it ends up closing the app.

In the class of service I execute the following code:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("onStartCommand", "onStartCommand");
    try {
        try {
            this.MQTTServer = new MQTTServer(this);
            this.MQTTServer.init(new GenericCode.BooleanCode() {
                @Override
                public void booleanCode(boolean connected) {}
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    } catch(Exception e){
        e.printStackTrace();
    }

    return super.onStartCommand(intent, flags, startId);
}

Error message when the server is not running:

2020-08-31 11:05:33.172 3099-3099/com.example.ktrine E/ActivityThread: Service com.example.ktrine.service.NotificationService has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@8c9622a that was originally bound here
android.app.ServiceConnectionLeaked: Service com.example.ktrine.service.NotificationService has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@8c9622a that was originally bound here
    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1541)
    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1433)
    at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1620)
    at android.app.ContextImpl.bindService(ContextImpl.java:1572)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:684)
    at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:420)
    at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:354)
    at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:309)
    at com.example.ktrine.server.MQTTServer.init(MQTTServer.java:34)
    at com.example.ktrine.service.NotificationService.onStartCommand(NotificationService.java:39)

Even though the code block has a try / catch, it is closing the app. Does this have anything to do with the service that is running in the background?

0 Answers
Related