问题描述
下午好,我创建了一个服务来监听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 (将#修改为@)