From 8da004c024200c446fb56948eb51acf45a894f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 14 Nov 2023 19:24:10 +0800 Subject: [PATCH] Fix notification --- app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt | 2 -- .../main/java/io/nekohasekai/sfa/bg/ServiceNotification.kt | 7 ++++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt b/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt index c78172a..e753d06 100644 --- a/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt +++ b/app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt @@ -17,7 +17,6 @@ import io.nekohasekai.libbox.BoxService import io.nekohasekai.libbox.CommandServer import io.nekohasekai.libbox.CommandServerHandler import io.nekohasekai.libbox.Libbox -import io.nekohasekai.libbox.PProfServer import io.nekohasekai.libbox.PlatformInterface import io.nekohasekai.libbox.SystemProxyStatus import io.nekohasekai.sfa.Application @@ -89,7 +88,6 @@ class BoxService( private val notification = ServiceNotification(status, service) private var boxService: BoxService? = null private var commandServer: CommandServer? = null - private var pprofServer: PProfServer? = null private var receiverRegistered = false private val receiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { diff --git a/app/src/main/java/io/nekohasekai/sfa/bg/ServiceNotification.kt b/app/src/main/java/io/nekohasekai/sfa/bg/ServiceNotification.kt index 0d58383..271e652 100644 --- a/app/src/main/java/io/nekohasekai/sfa/bg/ServiceNotification.kt +++ b/app/src/main/java/io/nekohasekai/sfa/bg/ServiceNotification.kt @@ -44,6 +44,7 @@ class ServiceNotification( private val commandClient = CommandClient(GlobalScope, CommandClient.ConnectionType.Status, this) + private var receiverRegistered = false private val notificationBuilder by lazy { NotificationCompat.Builder(service, notificationChannel).setShowWhen(false).setOngoing(true) @@ -103,6 +104,7 @@ class ServiceNotification( addAction(Intent.ACTION_SCREEN_ON) addAction(Intent.ACTION_SCREEN_OFF) }) + receiverRegistered = true } override fun updateStatus(status: StatusMessage) { @@ -129,6 +131,9 @@ class ServiceNotification( fun close() { commandClient.disconnect() ServiceCompat.stopForeground(service, ServiceCompat.STOP_FOREGROUND_REMOVE) - service.unregisterReceiver(this) + if (receiverRegistered) { + service.unregisterReceiver(this) + receiverRegistered = false + } } } \ No newline at end of file