From 26c2ecef6d7277df6120cbb2392c647e54c76315 Mon Sep 17 00:00:00 2001 From: Wang Han <1701210638@pku.edu.cn> Date: Fri, 30 Aug 2024 22:53:53 +0800 Subject: [PATCH] Avoid starting foreground service repeatedly --- .../clash/service/clash/module/DynamicNotificationModule.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/DynamicNotificationModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/DynamicNotificationModule.kt index 7dfefd074..43aa3d659 100644 --- a/service/src/main/java/com/github/kr328/clash/service/clash/module/DynamicNotificationModule.kt +++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/DynamicNotificationModule.kt @@ -5,6 +5,7 @@ import android.app.Service import android.content.Intent import android.os.PowerManager import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat import androidx.core.content.getSystemService import com.github.kr328.clash.common.compat.getColorCompat import com.github.kr328.clash.common.compat.pendingIntentFlags @@ -40,6 +41,8 @@ class DynamicNotificationModule(service: Service) : Module(service) { ) ) + private val notificationManager = NotificationManagerCompat.from(service) + private fun update() { val now = Clash.queryTrafficNow() val total = Clash.queryTrafficTotal() @@ -64,7 +67,7 @@ class DynamicNotificationModule(service: Service) : Module(service) { ) .build() - service.startForeground(R.id.nf_clash_status, notification) + notificationManager.notify(R.id.nf_clash_status, notification) } override suspend fun run() = coroutineScope {