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