Răsfoiți Sursa

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

Antecer 2 ani în urmă
părinte
comite
921937cc5a
1 a modificat fișierele cu 9 adăugiri și 8 ștergeri
  1. 9 8
      app/src/main/java/io/legado/app/service/WebService.kt

+ 9 - 8
app/src/main/java/io/legado/app/service/WebService.kt

@@ -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()) }
-                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()
                 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()) }
-                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
                 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")
             )
             )