diff --git a/app/src/main/java/com/casic/app/safetreecontroller/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/app/safetreecontroller/service/ForegroundRunningService.kt index 956ca38..9e96cd0 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/service/ForegroundRunningService.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/service/ForegroundRunningService.kt @@ -49,30 +49,20 @@ } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - weakReferenceHandler = WeakReferenceHandler(this) + if (weakReferenceHandler == null) { + weakReferenceHandler = WeakReferenceHandler(this) + } return START_STICKY } override fun handleMessage(msg: Message): Boolean { when (msg.what) { - 2024110501 -> { - notificationBuilder?.setContentTitle("安全树通讯服务已连接") - val notification = notificationBuilder?.build() - notificationManager?.notify(notificationId, notification) - } - - 2024110502 -> { - notificationBuilder?.setContentTitle("安全树通讯服务断开连接,开始重连") - val notification = notificationBuilder?.build() - notificationManager?.notify(notificationId, notification) - } - - 2024110503 -> { - notificationBuilder?.setContentTitle("安全树通讯服务连接出错,开始重连") - val notification = notificationBuilder?.build() - notificationManager?.notify(notificationId, notification) - } + 2024110501 -> notificationBuilder?.setContentTitle("安全树通讯服务已连接") + 2024110502 -> notificationBuilder?.setContentTitle("安全树通讯服务断开连接,开始重连") + 2024110503 -> notificationBuilder?.setContentTitle("安全树通讯服务连接出错,开始重连") } + val notification = notificationBuilder?.build() + notificationManager?.notify(notificationId, notification) return true } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/app/safetreecontroller/service/ForegroundRunningService.kt index 956ca38..9e96cd0 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/service/ForegroundRunningService.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/service/ForegroundRunningService.kt @@ -49,30 +49,20 @@ } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - weakReferenceHandler = WeakReferenceHandler(this) + if (weakReferenceHandler == null) { + weakReferenceHandler = WeakReferenceHandler(this) + } return START_STICKY } override fun handleMessage(msg: Message): Boolean { when (msg.what) { - 2024110501 -> { - notificationBuilder?.setContentTitle("安全树通讯服务已连接") - val notification = notificationBuilder?.build() - notificationManager?.notify(notificationId, notification) - } - - 2024110502 -> { - notificationBuilder?.setContentTitle("安全树通讯服务断开连接,开始重连") - val notification = notificationBuilder?.build() - notificationManager?.notify(notificationId, notification) - } - - 2024110503 -> { - notificationBuilder?.setContentTitle("安全树通讯服务连接出错,开始重连") - val notification = notificationBuilder?.build() - notificationManager?.notify(notificationId, notification) - } + 2024110501 -> notificationBuilder?.setContentTitle("安全树通讯服务已连接") + 2024110502 -> notificationBuilder?.setContentTitle("安全树通讯服务断开连接,开始重连") + 2024110503 -> notificationBuilder?.setContentTitle("安全树通讯服务连接出错,开始重连") } + val notification = notificationBuilder?.build() + notificationManager?.notify(notificationId, notification) return true } diff --git a/app/src/main/java/com/casic/app/safetreecontroller/tcp/TcpClient.kt b/app/src/main/java/com/casic/app/safetreecontroller/tcp/TcpClient.kt index b002415..f7d27b5 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/tcp/TcpClient.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/tcp/TcpClient.kt @@ -57,6 +57,7 @@ .option( ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator(5000, 5000, 8000) ) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) .handler(SimpleChannelInitializer()) connect() } @@ -67,7 +68,7 @@ pipeline() .addLast(ByteArrayDecoder()) .addLast(ByteArrayEncoder()) - .addLast(IdleStateHandler(60, 10, 0)) + .addLast(IdleStateHandler(0, 0, 60))//如果连接没有接收或发送数据超过60秒钟就发送一次心跳 .addLast(object : SimpleChannelInboundHandler() { override fun channelActive(ctx: ChannelHandlerContext) { val address = ctx.channel().remoteAddress() as InetSocketAddress @@ -82,15 +83,11 @@ reconnect() } - override fun channelRead0( - ctx: ChannelHandlerContext, msg: ByteArray? - ) { + override fun channelRead0(ctx: ChannelHandlerContext, msg: ByteArray?) { listener.onMessageReceived(msg) } - override fun exceptionCaught( - ctx: ChannelHandlerContext, cause: Throwable - ) { + override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) { Log.d(kTag, "exceptionCaught: ${cause.message}") listener.onConnectFailed() ctx.close()