diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cd435fd..2dbaa56 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + @@ -85,7 +86,8 @@ - + + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cd435fd..2dbaa56 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + @@ -85,7 +86,8 @@ - + + diff --git a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt new file mode 100644 index 0000000..a3b504f --- /dev/null +++ b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt @@ -0,0 +1,81 @@ +package com.casic.br.operationsite.service + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.Service +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.Handler +import android.os.IBinder +import android.os.Message +import androidx.core.app.NotificationCompat +import com.casic.br.operationsite.R +import com.pengxh.kt.lite.utils.WeakReferenceHandler + +class ForegroundRunningService : Service(), Handler.Callback { + + companion object { + var weakReferenceHandler: WeakReferenceHandler? = null + } + + private val kTag = "ForegroundRunningService" + private val notificationId = 1 + private var notificationManager: NotificationManager? = null + private var notificationBuilder: NotificationCompat.Builder? = null + + override fun onCreate() { + super.onCreate() + notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + // Android 8.0(API 级别 26)及以上版本需要创建通知渠道 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val name = "${resources.getString(R.string.app_name)}前台服务" + val channel = NotificationChannel( + "foreground_running_service_channel", name, NotificationManager.IMPORTANCE_HIGH + ) + channel.description = "Channel for Foreground Running Service" + notificationManager?.createNotificationChannel(channel) + } + notificationBuilder = NotificationCompat.Builder(this, "foreground_running_service_channel") + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("通讯服务连接中...") + .setContentText("为降低被系统杀死的概率,请勿关闭此通知") + .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 + .setOngoing(true) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + + val notification = notificationBuilder?.build() + startForeground(notificationId, notification) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (weakReferenceHandler == null) { + weakReferenceHandler = WeakReferenceHandler(this) + } + return START_STICKY + } + + override fun handleMessage(msg: Message): Boolean { + when (msg.what) { + 2024110501 -> notificationBuilder?.setContentTitle("通讯服务已连接") + 2024110502 -> notificationBuilder?.setContentTitle("通讯服务断开连接,开始重连") + 2024110503 -> notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + } + val notification = notificationBuilder?.build() + notificationManager?.notify(notificationId, notification) + return true + } + + override fun onDestroy() { + super.onDestroy() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + stopForeground(STOP_FOREGROUND_REMOVE) + } else { + stopForeground(true) + } + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cd435fd..2dbaa56 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + @@ -85,7 +86,8 @@ - + + diff --git a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt new file mode 100644 index 0000000..a3b504f --- /dev/null +++ b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt @@ -0,0 +1,81 @@ +package com.casic.br.operationsite.service + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.Service +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.Handler +import android.os.IBinder +import android.os.Message +import androidx.core.app.NotificationCompat +import com.casic.br.operationsite.R +import com.pengxh.kt.lite.utils.WeakReferenceHandler + +class ForegroundRunningService : Service(), Handler.Callback { + + companion object { + var weakReferenceHandler: WeakReferenceHandler? = null + } + + private val kTag = "ForegroundRunningService" + private val notificationId = 1 + private var notificationManager: NotificationManager? = null + private var notificationBuilder: NotificationCompat.Builder? = null + + override fun onCreate() { + super.onCreate() + notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + // Android 8.0(API 级别 26)及以上版本需要创建通知渠道 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val name = "${resources.getString(R.string.app_name)}前台服务" + val channel = NotificationChannel( + "foreground_running_service_channel", name, NotificationManager.IMPORTANCE_HIGH + ) + channel.description = "Channel for Foreground Running Service" + notificationManager?.createNotificationChannel(channel) + } + notificationBuilder = NotificationCompat.Builder(this, "foreground_running_service_channel") + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("通讯服务连接中...") + .setContentText("为降低被系统杀死的概率,请勿关闭此通知") + .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 + .setOngoing(true) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + + val notification = notificationBuilder?.build() + startForeground(notificationId, notification) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (weakReferenceHandler == null) { + weakReferenceHandler = WeakReferenceHandler(this) + } + return START_STICKY + } + + override fun handleMessage(msg: Message): Boolean { + when (msg.what) { + 2024110501 -> notificationBuilder?.setContentTitle("通讯服务已连接") + 2024110502 -> notificationBuilder?.setContentTitle("通讯服务断开连接,开始重连") + 2024110503 -> notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + } + val notification = notificationBuilder?.build() + notificationManager?.notify(notificationId, notification) + return true + } + + override fun onDestroy() { + super.onDestroy() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + stopForeground(STOP_FOREGROUND_REMOVE) + } else { + stopForeground(true) + } + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt new file mode 100644 index 0000000..8b23c82 --- /dev/null +++ b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt @@ -0,0 +1,65 @@ +package com.casic.br.operationsite.service + +import android.app.Service +import android.content.Intent +import android.os.Handler +import android.os.IBinder +import android.os.Message +import android.util.Log +import com.casic.br.operationsite.utils.LocaleConstant +import com.pengxh.kt.lite.utils.WeakReferenceHandler +import com.pengxh.kt.lite.utils.socket.tcp.OnTcpConnectStateListener +import com.pengxh.kt.lite.utils.socket.tcp.TcpClient + +class SocketConnectionService : Service(), OnTcpConnectStateListener, Handler.Callback { + + companion object { + var weakReferenceHandler: WeakReferenceHandler? = null + } + + private val kTag = "SocketService" + private val tcpClient by lazy { TcpClient(this) } + + override fun handleMessage(msg: Message): Boolean { + when (msg.what) { + + } + return true + } + + override fun onCreate() { + super.onCreate() + weakReferenceHandler = WeakReferenceHandler(this) + tcpClient.start(LocaleConstant.GAS_BASE_IP, LocaleConstant.TCP_PORT) + Log.d(kTag, "onCreate: SocketConnectionService") + } + + override fun onConnected() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110501) + } + + override fun onDisconnected() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110502) + } + + override fun onConnectFailed() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110503) + } + + override fun onMessageReceived(bytes: ByteArray?) { + if (bytes == null) { + return + } + Log.d(kTag, bytes.contentToString()) + } + + override fun onDestroy() { + super.onDestroy() + tcpClient.stop() + Log.d(kTag, "onDestroy: SocketConnectionService") + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cd435fd..2dbaa56 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + @@ -85,7 +86,8 @@ - + + diff --git a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt new file mode 100644 index 0000000..a3b504f --- /dev/null +++ b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt @@ -0,0 +1,81 @@ +package com.casic.br.operationsite.service + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.Service +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.Handler +import android.os.IBinder +import android.os.Message +import androidx.core.app.NotificationCompat +import com.casic.br.operationsite.R +import com.pengxh.kt.lite.utils.WeakReferenceHandler + +class ForegroundRunningService : Service(), Handler.Callback { + + companion object { + var weakReferenceHandler: WeakReferenceHandler? = null + } + + private val kTag = "ForegroundRunningService" + private val notificationId = 1 + private var notificationManager: NotificationManager? = null + private var notificationBuilder: NotificationCompat.Builder? = null + + override fun onCreate() { + super.onCreate() + notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + // Android 8.0(API 级别 26)及以上版本需要创建通知渠道 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val name = "${resources.getString(R.string.app_name)}前台服务" + val channel = NotificationChannel( + "foreground_running_service_channel", name, NotificationManager.IMPORTANCE_HIGH + ) + channel.description = "Channel for Foreground Running Service" + notificationManager?.createNotificationChannel(channel) + } + notificationBuilder = NotificationCompat.Builder(this, "foreground_running_service_channel") + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("通讯服务连接中...") + .setContentText("为降低被系统杀死的概率,请勿关闭此通知") + .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 + .setOngoing(true) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + + val notification = notificationBuilder?.build() + startForeground(notificationId, notification) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (weakReferenceHandler == null) { + weakReferenceHandler = WeakReferenceHandler(this) + } + return START_STICKY + } + + override fun handleMessage(msg: Message): Boolean { + when (msg.what) { + 2024110501 -> notificationBuilder?.setContentTitle("通讯服务已连接") + 2024110502 -> notificationBuilder?.setContentTitle("通讯服务断开连接,开始重连") + 2024110503 -> notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + } + val notification = notificationBuilder?.build() + notificationManager?.notify(notificationId, notification) + return true + } + + override fun onDestroy() { + super.onDestroy() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + stopForeground(STOP_FOREGROUND_REMOVE) + } else { + stopForeground(true) + } + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt new file mode 100644 index 0000000..8b23c82 --- /dev/null +++ b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt @@ -0,0 +1,65 @@ +package com.casic.br.operationsite.service + +import android.app.Service +import android.content.Intent +import android.os.Handler +import android.os.IBinder +import android.os.Message +import android.util.Log +import com.casic.br.operationsite.utils.LocaleConstant +import com.pengxh.kt.lite.utils.WeakReferenceHandler +import com.pengxh.kt.lite.utils.socket.tcp.OnTcpConnectStateListener +import com.pengxh.kt.lite.utils.socket.tcp.TcpClient + +class SocketConnectionService : Service(), OnTcpConnectStateListener, Handler.Callback { + + companion object { + var weakReferenceHandler: WeakReferenceHandler? = null + } + + private val kTag = "SocketService" + private val tcpClient by lazy { TcpClient(this) } + + override fun handleMessage(msg: Message): Boolean { + when (msg.what) { + + } + return true + } + + override fun onCreate() { + super.onCreate() + weakReferenceHandler = WeakReferenceHandler(this) + tcpClient.start(LocaleConstant.GAS_BASE_IP, LocaleConstant.TCP_PORT) + Log.d(kTag, "onCreate: SocketConnectionService") + } + + override fun onConnected() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110501) + } + + override fun onDisconnected() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110502) + } + + override fun onConnectFailed() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110503) + } + + override fun onMessageReceived(bytes: ByteArray?) { + if (bytes == null) { + return + } + Log.d(kTag, bytes.contentToString()) + } + + override fun onDestroy() { + super.onDestroy() + tcpClient.stop() + Log.d(kTag, "onDestroy: SocketConnectionService") + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt b/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt index 209e159..5f03cfa 100644 --- a/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt +++ b/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt @@ -55,6 +55,7 @@ private var speed = 5 private var cameraIp = "" private var httpConfig = "" + private var isRtspPlaying = false @SuppressLint("ClickableViewAccessibility") override fun initEvent() { @@ -186,7 +187,13 @@ } binding.openVideoButton.setOnClickListener { - binding.rtspPlayerView.startPlayLogic() + isRtspPlaying = if (isRtspPlaying) { + binding.rtspPlayerView.release() + false + } else { + binding.rtspPlayerView.startPlayLogic() + true + } } binding.startCheckButton.setOnClickListener { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cd435fd..2dbaa56 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + @@ -85,7 +86,8 @@ - + + diff --git a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt new file mode 100644 index 0000000..a3b504f --- /dev/null +++ b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt @@ -0,0 +1,81 @@ +package com.casic.br.operationsite.service + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.Service +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.Handler +import android.os.IBinder +import android.os.Message +import androidx.core.app.NotificationCompat +import com.casic.br.operationsite.R +import com.pengxh.kt.lite.utils.WeakReferenceHandler + +class ForegroundRunningService : Service(), Handler.Callback { + + companion object { + var weakReferenceHandler: WeakReferenceHandler? = null + } + + private val kTag = "ForegroundRunningService" + private val notificationId = 1 + private var notificationManager: NotificationManager? = null + private var notificationBuilder: NotificationCompat.Builder? = null + + override fun onCreate() { + super.onCreate() + notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + // Android 8.0(API 级别 26)及以上版本需要创建通知渠道 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val name = "${resources.getString(R.string.app_name)}前台服务" + val channel = NotificationChannel( + "foreground_running_service_channel", name, NotificationManager.IMPORTANCE_HIGH + ) + channel.description = "Channel for Foreground Running Service" + notificationManager?.createNotificationChannel(channel) + } + notificationBuilder = NotificationCompat.Builder(this, "foreground_running_service_channel") + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("通讯服务连接中...") + .setContentText("为降低被系统杀死的概率,请勿关闭此通知") + .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 + .setOngoing(true) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + + val notification = notificationBuilder?.build() + startForeground(notificationId, notification) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (weakReferenceHandler == null) { + weakReferenceHandler = WeakReferenceHandler(this) + } + return START_STICKY + } + + override fun handleMessage(msg: Message): Boolean { + when (msg.what) { + 2024110501 -> notificationBuilder?.setContentTitle("通讯服务已连接") + 2024110502 -> notificationBuilder?.setContentTitle("通讯服务断开连接,开始重连") + 2024110503 -> notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + } + val notification = notificationBuilder?.build() + notificationManager?.notify(notificationId, notification) + return true + } + + override fun onDestroy() { + super.onDestroy() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + stopForeground(STOP_FOREGROUND_REMOVE) + } else { + stopForeground(true) + } + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt new file mode 100644 index 0000000..8b23c82 --- /dev/null +++ b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt @@ -0,0 +1,65 @@ +package com.casic.br.operationsite.service + +import android.app.Service +import android.content.Intent +import android.os.Handler +import android.os.IBinder +import android.os.Message +import android.util.Log +import com.casic.br.operationsite.utils.LocaleConstant +import com.pengxh.kt.lite.utils.WeakReferenceHandler +import com.pengxh.kt.lite.utils.socket.tcp.OnTcpConnectStateListener +import com.pengxh.kt.lite.utils.socket.tcp.TcpClient + +class SocketConnectionService : Service(), OnTcpConnectStateListener, Handler.Callback { + + companion object { + var weakReferenceHandler: WeakReferenceHandler? = null + } + + private val kTag = "SocketService" + private val tcpClient by lazy { TcpClient(this) } + + override fun handleMessage(msg: Message): Boolean { + when (msg.what) { + + } + return true + } + + override fun onCreate() { + super.onCreate() + weakReferenceHandler = WeakReferenceHandler(this) + tcpClient.start(LocaleConstant.GAS_BASE_IP, LocaleConstant.TCP_PORT) + Log.d(kTag, "onCreate: SocketConnectionService") + } + + override fun onConnected() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110501) + } + + override fun onDisconnected() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110502) + } + + override fun onConnectFailed() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110503) + } + + override fun onMessageReceived(bytes: ByteArray?) { + if (bytes == null) { + return + } + Log.d(kTag, bytes.contentToString()) + } + + override fun onDestroy() { + super.onDestroy() + tcpClient.stop() + Log.d(kTag, "onDestroy: SocketConnectionService") + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt b/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt index 209e159..5f03cfa 100644 --- a/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt +++ b/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt @@ -55,6 +55,7 @@ private var speed = 5 private var cameraIp = "" private var httpConfig = "" + private var isRtspPlaying = false @SuppressLint("ClickableViewAccessibility") override fun initEvent() { @@ -186,7 +187,13 @@ } binding.openVideoButton.setOnClickListener { - binding.rtspPlayerView.startPlayLogic() + isRtspPlaying = if (isRtspPlaying) { + binding.rtspPlayerView.release() + false + } else { + binding.rtspPlayerView.startPlayLogic() + true + } } binding.startCheckButton.setOnClickListener { diff --git a/app/src/main/java/com/casic/br/operationsite/view/MainActivity.kt b/app/src/main/java/com/casic/br/operationsite/view/MainActivity.kt index 3631e0c..903b9d0 100644 --- a/app/src/main/java/com/casic/br/operationsite/view/MainActivity.kt +++ b/app/src/main/java/com/casic/br/operationsite/view/MainActivity.kt @@ -69,6 +69,9 @@ } } obtainProjectListByPage() + +// startService(Intent(this, SocketConnectionService::class.java)) +// startService(Intent(this, ForegroundRunningService::class.java)) } override fun observeRequestState() { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cd435fd..2dbaa56 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + @@ -85,7 +86,8 @@ - + + diff --git a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt new file mode 100644 index 0000000..a3b504f --- /dev/null +++ b/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt @@ -0,0 +1,81 @@ +package com.casic.br.operationsite.service + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.Service +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.Handler +import android.os.IBinder +import android.os.Message +import androidx.core.app.NotificationCompat +import com.casic.br.operationsite.R +import com.pengxh.kt.lite.utils.WeakReferenceHandler + +class ForegroundRunningService : Service(), Handler.Callback { + + companion object { + var weakReferenceHandler: WeakReferenceHandler? = null + } + + private val kTag = "ForegroundRunningService" + private val notificationId = 1 + private var notificationManager: NotificationManager? = null + private var notificationBuilder: NotificationCompat.Builder? = null + + override fun onCreate() { + super.onCreate() + notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + // Android 8.0(API 级别 26)及以上版本需要创建通知渠道 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val name = "${resources.getString(R.string.app_name)}前台服务" + val channel = NotificationChannel( + "foreground_running_service_channel", name, NotificationManager.IMPORTANCE_HIGH + ) + channel.description = "Channel for Foreground Running Service" + notificationManager?.createNotificationChannel(channel) + } + notificationBuilder = NotificationCompat.Builder(this, "foreground_running_service_channel") + .setSmallIcon(R.mipmap.ic_launcher) + .setContentTitle("通讯服务连接中...") + .setContentText("为降低被系统杀死的概率,请勿关闭此通知") + .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级 + .setOngoing(true) + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + + val notification = notificationBuilder?.build() + startForeground(notificationId, notification) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (weakReferenceHandler == null) { + weakReferenceHandler = WeakReferenceHandler(this) + } + return START_STICKY + } + + override fun handleMessage(msg: Message): Boolean { + when (msg.what) { + 2024110501 -> notificationBuilder?.setContentTitle("通讯服务已连接") + 2024110502 -> notificationBuilder?.setContentTitle("通讯服务断开连接,开始重连") + 2024110503 -> notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连") + } + val notification = notificationBuilder?.build() + notificationManager?.notify(notificationId, notification) + return true + } + + override fun onDestroy() { + super.onDestroy() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + stopForeground(STOP_FOREGROUND_REMOVE) + } else { + stopForeground(true) + } + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt new file mode 100644 index 0000000..8b23c82 --- /dev/null +++ b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt @@ -0,0 +1,65 @@ +package com.casic.br.operationsite.service + +import android.app.Service +import android.content.Intent +import android.os.Handler +import android.os.IBinder +import android.os.Message +import android.util.Log +import com.casic.br.operationsite.utils.LocaleConstant +import com.pengxh.kt.lite.utils.WeakReferenceHandler +import com.pengxh.kt.lite.utils.socket.tcp.OnTcpConnectStateListener +import com.pengxh.kt.lite.utils.socket.tcp.TcpClient + +class SocketConnectionService : Service(), OnTcpConnectStateListener, Handler.Callback { + + companion object { + var weakReferenceHandler: WeakReferenceHandler? = null + } + + private val kTag = "SocketService" + private val tcpClient by lazy { TcpClient(this) } + + override fun handleMessage(msg: Message): Boolean { + when (msg.what) { + + } + return true + } + + override fun onCreate() { + super.onCreate() + weakReferenceHandler = WeakReferenceHandler(this) + tcpClient.start(LocaleConstant.GAS_BASE_IP, LocaleConstant.TCP_PORT) + Log.d(kTag, "onCreate: SocketConnectionService") + } + + override fun onConnected() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110501) + } + + override fun onDisconnected() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110502) + } + + override fun onConnectFailed() { + ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(2024110503) + } + + override fun onMessageReceived(bytes: ByteArray?) { + if (bytes == null) { + return + } + Log.d(kTag, bytes.contentToString()) + } + + override fun onDestroy() { + super.onDestroy() + tcpClient.stop() + Log.d(kTag, "onDestroy: SocketConnectionService") + } + + override fun onBind(intent: Intent?): IBinder? { + return null + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt b/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt index 209e159..5f03cfa 100644 --- a/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt +++ b/app/src/main/java/com/casic/br/operationsite/view/DeviceControlActivity.kt @@ -55,6 +55,7 @@ private var speed = 5 private var cameraIp = "" private var httpConfig = "" + private var isRtspPlaying = false @SuppressLint("ClickableViewAccessibility") override fun initEvent() { @@ -186,7 +187,13 @@ } binding.openVideoButton.setOnClickListener { - binding.rtspPlayerView.startPlayLogic() + isRtspPlaying = if (isRtspPlaying) { + binding.rtspPlayerView.release() + false + } else { + binding.rtspPlayerView.startPlayLogic() + true + } } binding.startCheckButton.setOnClickListener { diff --git a/app/src/main/java/com/casic/br/operationsite/view/MainActivity.kt b/app/src/main/java/com/casic/br/operationsite/view/MainActivity.kt index 3631e0c..903b9d0 100644 --- a/app/src/main/java/com/casic/br/operationsite/view/MainActivity.kt +++ b/app/src/main/java/com/casic/br/operationsite/view/MainActivity.kt @@ -69,6 +69,9 @@ } } obtainProjectListByPage() + +// startService(Intent(this, SocketConnectionService::class.java)) +// startService(Intent(this, ForegroundRunningService::class.java)) } override fun observeRequestState() { diff --git a/app/src/main/res/layout/activity_device_control.xml b/app/src/main/res/layout/activity_device_control.xml index c886271..dab4c79 100644 --- a/app/src/main/res/layout/activity_device_control.xml +++ b/app/src/main/res/layout/activity_device_control.xml @@ -36,91 +36,6 @@ android:layout_height="match_parent" /> - - - - - - - - - - - - - - - - - - - - - - - - - @@ -130,6 +45,91 @@ android:layout_height="wrap_content" android:orientation="vertical"> + + + + + + + + + + + + + + + + + + + + + + + + +