Skip to content

Commit

Permalink
Avoid starting foreground service repeatedly
Browse files Browse the repository at this point in the history
  • Loading branch information
aviraxp authored Aug 30, 2024
1 parent a9b10ac commit 2aab6ea
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,6 +41,8 @@ class DynamicNotificationModule(service: Service) : Module<Unit>(service) {
)
)

private val notificationManager = NotificationManagerCompat.from(service)

private fun update() {
val now = Clash.queryTrafficNow()
val total = Clash.queryTrafficTotal()
Expand All @@ -64,7 +67,7 @@ class DynamicNotificationModule(service: Service) : Module<Unit>(service) {
)
.build()

service.startForeground(R.id.nf_clash_status, notification)
notificationManager.notify(R.id.nf_clash_status, notification)
}

override suspend fun run() = coroutineScope {
Expand Down Expand Up @@ -102,4 +105,4 @@ class DynamicNotificationModule(service: Service) : Module<Unit>(service) {
}
}
}
}
}

0 comments on commit 2aab6ea

Please sign in to comment.