Avoid starting foreground service repeatedly

This commit is contained in:
Wang Han 2024-08-30 22:53:53 +08:00 committed by Wang Han
parent a9b10acac2
commit 26c2ecef6d

View File

@ -5,6 +5,7 @@ import android.app.Service
import android.content.Intent import android.content.Intent
import android.os.PowerManager import android.os.PowerManager
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import com.github.kr328.clash.common.compat.getColorCompat import com.github.kr328.clash.common.compat.getColorCompat
import com.github.kr328.clash.common.compat.pendingIntentFlags import com.github.kr328.clash.common.compat.pendingIntentFlags
@ -40,6 +41,8 @@ class DynamicNotificationModule(service: Service) : Module<Unit>(service) {
) )
) )
private val notificationManager = NotificationManagerCompat.from(service)
private fun update() { private fun update() {
val now = Clash.queryTrafficNow() val now = Clash.queryTrafficNow()
val total = Clash.queryTrafficTotal() val total = Clash.queryTrafficTotal()
@ -64,7 +67,7 @@ class DynamicNotificationModule(service: Service) : Module<Unit>(service) {
) )
.build() .build()
service.startForeground(R.id.nf_clash_status, notification) notificationManager.notify(R.id.nf_clash_status, notification)
} }
override suspend fun run() = coroutineScope { override suspend fun run() = coroutineScope {