如何解决尝试连接到MQTT服务器但服务器未运行时关闭应用程序
下午好,我创建了一个服务来监听MQTT Broker服务器,但是,当我启动该应用程序并且该服务器未处于活动状态时,它正在关闭该应用程序;当我激活MQTT服务器并打开该应用程序时,它可以正常运行。连接到服务器的代码块已经实现了try / catch,但即使这样最终还是关闭了应用程序。
在服务类中,我执行以下代码:
@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);
}
服务器未运行时的错误消息:
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)
即使代码块具有try / catch,它仍在关闭应用程序。这与在后台运行的服务有什么关系吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。