web服务通知栏支持多个地址

This commit is contained in:
Antecer 2024-04-07 00:01:08 +08:00
parent f15cbe2d73
commit 6ef3c02a13

View File

@ -60,6 +60,7 @@ class WebService : BaseService() {
}
private var httpServer: HttpServer? = null
private var webSocketServer: WebSocketServer? = null
private var notificationList = mutableListOf(appCtx.getString(R.string.service_starting))
private var notificationContent = appCtx.getString(R.string.service_starting)
private val networkChangedListener by lazy {
NetworkChangedListener(this)
@ -77,14 +78,14 @@ class WebService : BaseService() {
networkChangedListener.register()
networkChangedListener.onNetworkChanged = {
val addressList = NetworkUtils.getLocalIPAddress()
notificationList.clear()
if (addressList.any()) {
val hostList = addressList.map { address -> getString(R.string.http_ip, address.hostAddress, getPort()) }
hostAddress = hostList.first()
notificationContent = hostList.joinToString(separator = "\n")
notificationList.addAll(addressList.map { address -> getString(R.string.http_ip, address.hostAddress, getPort()) })
hostAddress = notificationList.first()
startForegroundNotification()
} else {
hostAddress = getString(R.string.network_connection_unavailable)
notificationContent = hostAddress
notificationList.add(hostAddress)
startForegroundNotification()
}
postEvent(EventBus.WEB_SERVICE, hostAddress)
@ -139,9 +140,9 @@ class WebService : BaseService() {
try {
httpServer?.start()
webSocketServer?.start(1000 * 30) // 通信超时设置
val hostList = addressList.map { address -> getString(R.string.http_ip, address.hostAddress, getPort()) }
hostAddress = hostList.first()
notificationContent = hostList.joinToString(separator = "\n")
notificationList.clear()
notificationList.addAll(addressList.map { address -> getString(R.string.http_ip, address.hostAddress, getPort()) })
hostAddress = notificationList.first()
isRun = true
postEvent(EventBus.WEB_SERVICE, hostAddress)
startForegroundNotification()
@ -172,7 +173,7 @@ class WebService : BaseService() {
.setSmallIcon(R.drawable.ic_web_service_noti)
.setOngoing(true)
.setContentTitle(getString(R.string.web_service))
.setContentText(notificationContent)
.setContentText(notificationList.joinToString("\n"))
.setContentIntent(
servicePendingIntent<WebService>("copyHostAddress")
)