Skip to content

Commit 26c2ece

Browse files
committed
Avoid starting foreground service repeatedly
1 parent a9b10ac commit 26c2ece

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

service/src/main/java/com/github/kr328/clash/service/clash/module/DynamicNotificationModule.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.app.Service
55
import android.content.Intent
66
import android.os.PowerManager
77
import androidx.core.app.NotificationCompat
8+
import androidx.core.app.NotificationManagerCompat
89
import androidx.core.content.getSystemService
910
import com.github.kr328.clash.common.compat.getColorCompat
1011
import com.github.kr328.clash.common.compat.pendingIntentFlags
@@ -40,6 +41,8 @@ class DynamicNotificationModule(service: Service) : Module<Unit>(service) {
4041
)
4142
)
4243

44+
private val notificationManager = NotificationManagerCompat.from(service)
45+
4346
private fun update() {
4447
val now = Clash.queryTrafficNow()
4548
val total = Clash.queryTrafficTotal()
@@ -64,7 +67,7 @@ class DynamicNotificationModule(service: Service) : Module<Unit>(service) {
6467
)
6568
.build()
6669

67-
service.startForeground(R.id.nf_clash_status, notification)
70+
notificationManager.notify(R.id.nf_clash_status, notification)
6871
}
6972

7073
override suspend fun run() = coroutineScope {

0 commit comments

Comments
 (0)