android - 尝试连接到 MQTT 服务器但服务器未运行时关闭应用程序
问题描述
下午好,我创建了一个服务来监听 MQTT 代理服务器,但是当我启动应用程序并且服务器未处于活动状态时,它正在关闭应用程序,当我激活 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,它也会关闭应用程序。这与在后台运行的服务有什么关系吗?
解决方案
推荐阅读
- caching - 如何强制加载当前版本的离线网络应用程序?
- asp.net - 仅在文本框中对数字和“,”字符使用 RegularExpressionValidator
- excel - 在漏斗图中更改轴标签字体大小 (Excel 2016)
- excel - 为什么单元格不恢复为 ActiveCell.FormulaR1C1?
- r - 多个数据框到多个 html 表,但都一个接一个地显示
- javascript - 泄漏的原因是什么?
- java - Elasticsearch - 在查询中将数组作为参数传递
- ios - 更新后结构崩溃:[Fabric isCrashlyticsCollectionEnabled]:无法识别的选择器发送到类 0x10c0bb918
- c - 在 ubuntu 18.04 上的 c 文件中未定义对“readline”的引用(已安装 libreadline-dev,与“-lreadline”链接
- c# - .NET CORE(在 Windows 上),DataReceived 调用一个serialPort 的频率受其他serialPort 影响