diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 8ee6726..35c9024 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -88,9 +88,8 @@
-
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 8ee6726..35c9024 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -88,9 +88,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
deleted file mode 100644
index 1a27231..0000000
--- a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt
+++ /dev/null
@@ -1,116 +0,0 @@
-package com.casic.br.operationsite.service
-
-import android.app.NotificationChannel
-import android.app.NotificationManager
-import android.app.Service
-import android.content.Intent
-import android.media.AudioAttributes
-import android.media.SoundPool
-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.casic.br.operationsite.utils.LocaleConstant
-import com.pengxh.kt.lite.utils.WeakReferenceHandler
-
-class ForegroundRunningService : Service(), Handler.Callback {
-
- companion object {
- var weakReferenceHandler: WeakReferenceHandler? = null
- }
-
- private val notificationId = 1
- private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager }
- private var notificationBuilder: NotificationCompat.Builder? = null
- private val attr = AudioAttributes.Builder()
- .setUsage(AudioAttributes.USAGE_ALARM)
- .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
- .build()
- private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build()
- private var soundResourceId = 0
- private var isSoundLoaded = false
- private var isAlarmPlaying = false
-
- override fun onCreate() {
- super.onCreate()
- 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)
-
- soundPool.setOnLoadCompleteListener { soundPool, sampleId, status ->
- if (status == 0) {
- isSoundLoaded = true
- }
- }
- soundResourceId = soundPool.load(this, R.raw.ring, 1)
- }
-
- 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) {
- LocaleConstant.SOCKET_CONNECTED_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务已连接")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.SOCKET_DISCONNECTED_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务断开连接")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.SOCKET_CONNECT_ERROR_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.START_WARNING_CODE -> {
- if (isSoundLoaded && !isAlarmPlaying) {
- soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f)
- isAlarmPlaying = true
- }
- }
-
- LocaleConstant.STOP_WARNING_CODE -> {
- if (isAlarmPlaying) {
- soundPool.autoPause()
- isAlarmPlaying = false
- }
- }
- }
- return true
- }
-
- override fun onDestroy() {
- super.onDestroy()
- soundPool.release()
- soundPool = null
- stopForeground(STOP_FOREGROUND_REMOVE)
- }
-
- 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 8ee6726..35c9024 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -88,9 +88,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
deleted file mode 100644
index 1a27231..0000000
--- a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt
+++ /dev/null
@@ -1,116 +0,0 @@
-package com.casic.br.operationsite.service
-
-import android.app.NotificationChannel
-import android.app.NotificationManager
-import android.app.Service
-import android.content.Intent
-import android.media.AudioAttributes
-import android.media.SoundPool
-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.casic.br.operationsite.utils.LocaleConstant
-import com.pengxh.kt.lite.utils.WeakReferenceHandler
-
-class ForegroundRunningService : Service(), Handler.Callback {
-
- companion object {
- var weakReferenceHandler: WeakReferenceHandler? = null
- }
-
- private val notificationId = 1
- private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager }
- private var notificationBuilder: NotificationCompat.Builder? = null
- private val attr = AudioAttributes.Builder()
- .setUsage(AudioAttributes.USAGE_ALARM)
- .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
- .build()
- private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build()
- private var soundResourceId = 0
- private var isSoundLoaded = false
- private var isAlarmPlaying = false
-
- override fun onCreate() {
- super.onCreate()
- 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)
-
- soundPool.setOnLoadCompleteListener { soundPool, sampleId, status ->
- if (status == 0) {
- isSoundLoaded = true
- }
- }
- soundResourceId = soundPool.load(this, R.raw.ring, 1)
- }
-
- 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) {
- LocaleConstant.SOCKET_CONNECTED_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务已连接")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.SOCKET_DISCONNECTED_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务断开连接")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.SOCKET_CONNECT_ERROR_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.START_WARNING_CODE -> {
- if (isSoundLoaded && !isAlarmPlaying) {
- soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f)
- isAlarmPlaying = true
- }
- }
-
- LocaleConstant.STOP_WARNING_CODE -> {
- if (isAlarmPlaying) {
- soundPool.autoPause()
- isAlarmPlaying = false
- }
- }
- }
- return true
- }
-
- override fun onDestroy() {
- super.onDestroy()
- soundPool.release()
- soundPool = null
- stopForeground(STOP_FOREGROUND_REMOVE)
- }
-
- 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
index fd94f61..cf8feaf 100644
--- a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt
+++ b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt
@@ -1,11 +1,17 @@
package com.casic.br.operationsite.service
+import android.app.NotificationChannel
+import android.app.NotificationManager
import android.app.Service
import android.content.Intent
+import android.media.AudioAttributes
+import android.media.SoundPool
import android.os.Handler
import android.os.IBinder
import android.os.Message
import android.util.Log
+import androidx.core.app.NotificationCompat
+import com.casic.br.operationsite.R
import com.casic.br.operationsite.extensions.handleCpuTemperature
import com.casic.br.operationsite.extensions.handleGasConcentration
import com.casic.br.operationsite.extensions.toHex
@@ -27,7 +33,18 @@
}
private val kTag = "SocketService"
+ private val notificationId = 1
private val tcpClient by lazy { TcpClient(this) }
+ private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager }
+ private val attr = AudioAttributes.Builder()
+ .setUsage(AudioAttributes.USAGE_ALARM)
+ .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
+ .build()
+ private var notificationBuilder: NotificationCompat.Builder? = null
+ private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build()
+ private var soundResourceId = 0
+ private var isSoundLoaded = false
+ private var isAlarmPlaying = false
override fun handleMessage(msg: Message): Boolean {
when (msg.what) {
@@ -151,10 +168,35 @@
override fun onCreate() {
super.onCreate()
- weakReferenceHandler = WeakReferenceHandler(this)
+ 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)
+
+ soundPool.setOnLoadCompleteListener { soundPool, sampleId, status ->
+ if (status == 0) {
+ isSoundLoaded = true
+ }
+ }
+ soundResourceId = soundPool.load(this, R.raw.ring, 1)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
+ if (weakReferenceHandler == null) {
+ weakReferenceHandler = WeakReferenceHandler(this)
+ }
val socketOldIp = SaveKeyValues.getValue(
LocaleConstant.SAFE_TREE_SOCKET_IP_KEY, ""
) as String
@@ -164,18 +206,24 @@
}
override fun onConnected() {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECTED_CODE)
+ notificationBuilder?.setContentTitle("通讯服务已连接")
+ val notification = notificationBuilder?.build()
+ notificationManager.notify(notificationId, notification)
//定时查询甲烷浓度
weakReferenceHandler?.post(methaneRunnable)
}
override fun onDisconnected() {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_DISCONNECTED_CODE)
+ notificationBuilder?.setContentTitle("通讯服务断开连接")
+ val notification = notificationBuilder?.build()
+ notificationManager.notify(notificationId, notification)
weakReferenceHandler?.removeCallbacks(methaneRunnable)
}
override fun onConnectFailed() {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECT_ERROR_CODE)
+ notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连")
+ val notification = notificationBuilder?.build()
+ notificationManager.notify(notificationId, notification)
weakReferenceHandler?.removeCallbacks(methaneRunnable)
}
@@ -264,13 +312,15 @@
LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 2.00f
) as Float
if (concentration.toFloat() >= threshold) {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(
- LocaleConstant.START_WARNING_CODE
- )
+ if (isSoundLoaded && !isAlarmPlaying) {
+ soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f)
+ isAlarmPlaying = true
+ }
} else {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(
- LocaleConstant.STOP_WARNING_CODE
- )
+ if (isAlarmPlaying) {
+ soundPool.autoPause()
+ isAlarmPlaying = false
+ }
}
val weakReferenceHandler = MethaneMonitorFragment.weakReferenceHandler ?: return
val message = weakReferenceHandler.obtainMessage()
@@ -366,6 +416,9 @@
super.onDestroy()
weakReferenceHandler?.removeCallbacks(methaneRunnable)
tcpClient.stop(false)
+ soundPool.release()
+ soundPool = null
+ stopForeground(STOP_FOREGROUND_REMOVE)
Log.d(kTag, "onDestroy: SocketConnectionService")
}
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 8ee6726..35c9024 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -88,9 +88,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
deleted file mode 100644
index 1a27231..0000000
--- a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt
+++ /dev/null
@@ -1,116 +0,0 @@
-package com.casic.br.operationsite.service
-
-import android.app.NotificationChannel
-import android.app.NotificationManager
-import android.app.Service
-import android.content.Intent
-import android.media.AudioAttributes
-import android.media.SoundPool
-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.casic.br.operationsite.utils.LocaleConstant
-import com.pengxh.kt.lite.utils.WeakReferenceHandler
-
-class ForegroundRunningService : Service(), Handler.Callback {
-
- companion object {
- var weakReferenceHandler: WeakReferenceHandler? = null
- }
-
- private val notificationId = 1
- private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager }
- private var notificationBuilder: NotificationCompat.Builder? = null
- private val attr = AudioAttributes.Builder()
- .setUsage(AudioAttributes.USAGE_ALARM)
- .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
- .build()
- private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build()
- private var soundResourceId = 0
- private var isSoundLoaded = false
- private var isAlarmPlaying = false
-
- override fun onCreate() {
- super.onCreate()
- 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)
-
- soundPool.setOnLoadCompleteListener { soundPool, sampleId, status ->
- if (status == 0) {
- isSoundLoaded = true
- }
- }
- soundResourceId = soundPool.load(this, R.raw.ring, 1)
- }
-
- 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) {
- LocaleConstant.SOCKET_CONNECTED_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务已连接")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.SOCKET_DISCONNECTED_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务断开连接")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.SOCKET_CONNECT_ERROR_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.START_WARNING_CODE -> {
- if (isSoundLoaded && !isAlarmPlaying) {
- soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f)
- isAlarmPlaying = true
- }
- }
-
- LocaleConstant.STOP_WARNING_CODE -> {
- if (isAlarmPlaying) {
- soundPool.autoPause()
- isAlarmPlaying = false
- }
- }
- }
- return true
- }
-
- override fun onDestroy() {
- super.onDestroy()
- soundPool.release()
- soundPool = null
- stopForeground(STOP_FOREGROUND_REMOVE)
- }
-
- 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
index fd94f61..cf8feaf 100644
--- a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt
+++ b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt
@@ -1,11 +1,17 @@
package com.casic.br.operationsite.service
+import android.app.NotificationChannel
+import android.app.NotificationManager
import android.app.Service
import android.content.Intent
+import android.media.AudioAttributes
+import android.media.SoundPool
import android.os.Handler
import android.os.IBinder
import android.os.Message
import android.util.Log
+import androidx.core.app.NotificationCompat
+import com.casic.br.operationsite.R
import com.casic.br.operationsite.extensions.handleCpuTemperature
import com.casic.br.operationsite.extensions.handleGasConcentration
import com.casic.br.operationsite.extensions.toHex
@@ -27,7 +33,18 @@
}
private val kTag = "SocketService"
+ private val notificationId = 1
private val tcpClient by lazy { TcpClient(this) }
+ private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager }
+ private val attr = AudioAttributes.Builder()
+ .setUsage(AudioAttributes.USAGE_ALARM)
+ .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
+ .build()
+ private var notificationBuilder: NotificationCompat.Builder? = null
+ private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build()
+ private var soundResourceId = 0
+ private var isSoundLoaded = false
+ private var isAlarmPlaying = false
override fun handleMessage(msg: Message): Boolean {
when (msg.what) {
@@ -151,10 +168,35 @@
override fun onCreate() {
super.onCreate()
- weakReferenceHandler = WeakReferenceHandler(this)
+ 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)
+
+ soundPool.setOnLoadCompleteListener { soundPool, sampleId, status ->
+ if (status == 0) {
+ isSoundLoaded = true
+ }
+ }
+ soundResourceId = soundPool.load(this, R.raw.ring, 1)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
+ if (weakReferenceHandler == null) {
+ weakReferenceHandler = WeakReferenceHandler(this)
+ }
val socketOldIp = SaveKeyValues.getValue(
LocaleConstant.SAFE_TREE_SOCKET_IP_KEY, ""
) as String
@@ -164,18 +206,24 @@
}
override fun onConnected() {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECTED_CODE)
+ notificationBuilder?.setContentTitle("通讯服务已连接")
+ val notification = notificationBuilder?.build()
+ notificationManager.notify(notificationId, notification)
//定时查询甲烷浓度
weakReferenceHandler?.post(methaneRunnable)
}
override fun onDisconnected() {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_DISCONNECTED_CODE)
+ notificationBuilder?.setContentTitle("通讯服务断开连接")
+ val notification = notificationBuilder?.build()
+ notificationManager.notify(notificationId, notification)
weakReferenceHandler?.removeCallbacks(methaneRunnable)
}
override fun onConnectFailed() {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECT_ERROR_CODE)
+ notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连")
+ val notification = notificationBuilder?.build()
+ notificationManager.notify(notificationId, notification)
weakReferenceHandler?.removeCallbacks(methaneRunnable)
}
@@ -264,13 +312,15 @@
LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 2.00f
) as Float
if (concentration.toFloat() >= threshold) {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(
- LocaleConstant.START_WARNING_CODE
- )
+ if (isSoundLoaded && !isAlarmPlaying) {
+ soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f)
+ isAlarmPlaying = true
+ }
} else {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(
- LocaleConstant.STOP_WARNING_CODE
- )
+ if (isAlarmPlaying) {
+ soundPool.autoPause()
+ isAlarmPlaying = false
+ }
}
val weakReferenceHandler = MethaneMonitorFragment.weakReferenceHandler ?: return
val message = weakReferenceHandler.obtainMessage()
@@ -366,6 +416,9 @@
super.onDestroy()
weakReferenceHandler?.removeCallbacks(methaneRunnable)
tcpClient.stop(false)
+ soundPool.release()
+ soundPool = null
+ stopForeground(STOP_FOREGROUND_REMOVE)
Log.d(kTag, "onDestroy: SocketConnectionService")
}
diff --git a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt
index 340529a..78e3ace 100644
--- a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt
+++ b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt
@@ -65,10 +65,6 @@
const val PLAY_RTSP_CODE = 20241001
- const val SOCKET_CONNECTED_CODE = 2024110501
- const val SOCKET_DISCONNECTED_CODE = 2024110502
- const val SOCKET_CONNECT_ERROR_CODE = 2024110503
-
/**
* Handler Request Code
* */
@@ -116,9 +112,6 @@
const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241014
const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241015
- const val START_WARNING_CODE = 2025052801
- const val STOP_WARNING_CODE = 2025052802
-
/***
* SP Key
* */
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 8ee6726..35c9024 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -88,9 +88,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
deleted file mode 100644
index 1a27231..0000000
--- a/app/src/main/java/com/casic/br/operationsite/service/ForegroundRunningService.kt
+++ /dev/null
@@ -1,116 +0,0 @@
-package com.casic.br.operationsite.service
-
-import android.app.NotificationChannel
-import android.app.NotificationManager
-import android.app.Service
-import android.content.Intent
-import android.media.AudioAttributes
-import android.media.SoundPool
-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.casic.br.operationsite.utils.LocaleConstant
-import com.pengxh.kt.lite.utils.WeakReferenceHandler
-
-class ForegroundRunningService : Service(), Handler.Callback {
-
- companion object {
- var weakReferenceHandler: WeakReferenceHandler? = null
- }
-
- private val notificationId = 1
- private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager }
- private var notificationBuilder: NotificationCompat.Builder? = null
- private val attr = AudioAttributes.Builder()
- .setUsage(AudioAttributes.USAGE_ALARM)
- .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
- .build()
- private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build()
- private var soundResourceId = 0
- private var isSoundLoaded = false
- private var isAlarmPlaying = false
-
- override fun onCreate() {
- super.onCreate()
- 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)
-
- soundPool.setOnLoadCompleteListener { soundPool, sampleId, status ->
- if (status == 0) {
- isSoundLoaded = true
- }
- }
- soundResourceId = soundPool.load(this, R.raw.ring, 1)
- }
-
- 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) {
- LocaleConstant.SOCKET_CONNECTED_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务已连接")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.SOCKET_DISCONNECTED_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务断开连接")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.SOCKET_CONNECT_ERROR_CODE -> {
- notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连")
- val notification = notificationBuilder?.build()
- notificationManager.notify(notificationId, notification)
- }
-
- LocaleConstant.START_WARNING_CODE -> {
- if (isSoundLoaded && !isAlarmPlaying) {
- soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f)
- isAlarmPlaying = true
- }
- }
-
- LocaleConstant.STOP_WARNING_CODE -> {
- if (isAlarmPlaying) {
- soundPool.autoPause()
- isAlarmPlaying = false
- }
- }
- }
- return true
- }
-
- override fun onDestroy() {
- super.onDestroy()
- soundPool.release()
- soundPool = null
- stopForeground(STOP_FOREGROUND_REMOVE)
- }
-
- 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
index fd94f61..cf8feaf 100644
--- a/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt
+++ b/app/src/main/java/com/casic/br/operationsite/service/SocketConnectionService.kt
@@ -1,11 +1,17 @@
package com.casic.br.operationsite.service
+import android.app.NotificationChannel
+import android.app.NotificationManager
import android.app.Service
import android.content.Intent
+import android.media.AudioAttributes
+import android.media.SoundPool
import android.os.Handler
import android.os.IBinder
import android.os.Message
import android.util.Log
+import androidx.core.app.NotificationCompat
+import com.casic.br.operationsite.R
import com.casic.br.operationsite.extensions.handleCpuTemperature
import com.casic.br.operationsite.extensions.handleGasConcentration
import com.casic.br.operationsite.extensions.toHex
@@ -27,7 +33,18 @@
}
private val kTag = "SocketService"
+ private val notificationId = 1
private val tcpClient by lazy { TcpClient(this) }
+ private val notificationManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager }
+ private val attr = AudioAttributes.Builder()
+ .setUsage(AudioAttributes.USAGE_ALARM)
+ .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
+ .build()
+ private var notificationBuilder: NotificationCompat.Builder? = null
+ private var soundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(attr).build()
+ private var soundResourceId = 0
+ private var isSoundLoaded = false
+ private var isAlarmPlaying = false
override fun handleMessage(msg: Message): Boolean {
when (msg.what) {
@@ -151,10 +168,35 @@
override fun onCreate() {
super.onCreate()
- weakReferenceHandler = WeakReferenceHandler(this)
+ 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)
+
+ soundPool.setOnLoadCompleteListener { soundPool, sampleId, status ->
+ if (status == 0) {
+ isSoundLoaded = true
+ }
+ }
+ soundResourceId = soundPool.load(this, R.raw.ring, 1)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
+ if (weakReferenceHandler == null) {
+ weakReferenceHandler = WeakReferenceHandler(this)
+ }
val socketOldIp = SaveKeyValues.getValue(
LocaleConstant.SAFE_TREE_SOCKET_IP_KEY, ""
) as String
@@ -164,18 +206,24 @@
}
override fun onConnected() {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECTED_CODE)
+ notificationBuilder?.setContentTitle("通讯服务已连接")
+ val notification = notificationBuilder?.build()
+ notificationManager.notify(notificationId, notification)
//定时查询甲烷浓度
weakReferenceHandler?.post(methaneRunnable)
}
override fun onDisconnected() {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_DISCONNECTED_CODE)
+ notificationBuilder?.setContentTitle("通讯服务断开连接")
+ val notification = notificationBuilder?.build()
+ notificationManager.notify(notificationId, notification)
weakReferenceHandler?.removeCallbacks(methaneRunnable)
}
override fun onConnectFailed() {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(LocaleConstant.SOCKET_CONNECT_ERROR_CODE)
+ notificationBuilder?.setContentTitle("通讯服务连接出错,开始重连")
+ val notification = notificationBuilder?.build()
+ notificationManager.notify(notificationId, notification)
weakReferenceHandler?.removeCallbacks(methaneRunnable)
}
@@ -264,13 +312,15 @@
LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 2.00f
) as Float
if (concentration.toFloat() >= threshold) {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(
- LocaleConstant.START_WARNING_CODE
- )
+ if (isSoundLoaded && !isAlarmPlaying) {
+ soundPool.play(soundResourceId, 1f, 1f, 1, -1, 1f)
+ isAlarmPlaying = true
+ }
} else {
- ForegroundRunningService.weakReferenceHandler?.sendEmptyMessage(
- LocaleConstant.STOP_WARNING_CODE
- )
+ if (isAlarmPlaying) {
+ soundPool.autoPause()
+ isAlarmPlaying = false
+ }
}
val weakReferenceHandler = MethaneMonitorFragment.weakReferenceHandler ?: return
val message = weakReferenceHandler.obtainMessage()
@@ -366,6 +416,9 @@
super.onDestroy()
weakReferenceHandler?.removeCallbacks(methaneRunnable)
tcpClient.stop(false)
+ soundPool.release()
+ soundPool = null
+ stopForeground(STOP_FOREGROUND_REMOVE)
Log.d(kTag, "onDestroy: SocketConnectionService")
}
diff --git a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt
index 340529a..78e3ace 100644
--- a/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt
+++ b/app/src/main/java/com/casic/br/operationsite/utils/LocaleConstant.kt
@@ -65,10 +65,6 @@
const val PLAY_RTSP_CODE = 20241001
- const val SOCKET_CONNECTED_CODE = 2024110501
- const val SOCKET_DISCONNECTED_CODE = 2024110502
- const val SOCKET_CONNECT_ERROR_CODE = 2024110503
-
/**
* Handler Request Code
* */
@@ -116,9 +112,6 @@
const val SET_BOARD_WIFI_CONFIG_RESPONSE_CODE = 20241014
const val SET_BOARD_SERVER_CONFIG_RESPONSE_CODE = 20241015
- const val START_WARNING_CODE = 2025052801
- const val STOP_WARNING_CODE = 2025052802
-
/***
* SP Key
* */
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 06eaebe..827d98f 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
@@ -16,7 +16,6 @@
import com.casic.br.operationsite.fragments.DeviceControllerFragment
import com.casic.br.operationsite.fragments.MethaneMonitorFragment
import com.casic.br.operationsite.fragments.VoiceSettingsFragment
-import com.casic.br.operationsite.service.ForegroundRunningService
import com.casic.br.operationsite.service.SocketConnectionService
import com.casic.br.operationsite.utils.LocaleConstant
import com.casic.br.operationsite.utils.VideoPlayerManager
@@ -70,9 +69,6 @@
Intent(this, SocketConnectionService::class.java).apply {
startService(this)
}
- Intent(this, ForegroundRunningService::class.java).apply {
- startService(this)
- }
weakReferenceHandler = WeakReferenceHandler(this)
binding.viewPager.adapter = TabPageViewAdapter(