From 6f7b4a125f9d8991b06e63bdfcb99f97f69136b8 Mon Sep 17 00:00:00 2001 From: HystericalDragon Date: Wed, 31 Jan 2024 12:23:00 +0900 Subject: [PATCH] fix: fix reciver in SDK 34 --- app/src/main/AndroidManifest.xml | 12 ++++++++++- .../io/nekohasekai/sagernet/bg/BaseService.kt | 20 +++++++++++++++++-- .../sagernet/bg/ServiceNotification.kt | 6 +++++- .../sagernet/ui/VpnRequestActivity.kt | 11 +++++++++- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2d9c30f..4b1fc88 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,6 +13,7 @@ + @@ -254,11 +255,17 @@ + android:foregroundServiceType="specialUse" + android:process=":bg"> + + @@ -266,6 +273,9 @@ + = Build.VERSION_CODES.TIRAMISU) { + registerReceiver( + data.receiver, + filter, + "$packageName.SERVICE", + null, + Context.RECEIVER_NOT_EXPORTED + ) + } else { + registerReceiver( + data.receiver, + filter, + "$packageName.SERVICE", + null + ) + } data.closeReceiverRegistered = true } diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/ServiceNotification.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/ServiceNotification.kt index ed3a7de..5c19cc1 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/ServiceNotification.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/ServiceNotification.kt @@ -191,7 +191,11 @@ class ServiceNotification( fun destroy() { listenPostSpeed = false - (service as Service).stopForeground(true) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + (service as Service).stopForeground(Service.STOP_FOREGROUND_REMOVE) + } else { + (service as Service).stopForeground(true) + } service.unregisterReceiver(this) } } diff --git a/app/src/main/java/io/nekohasekai/sagernet/ui/VpnRequestActivity.kt b/app/src/main/java/io/nekohasekai/sagernet/ui/VpnRequestActivity.kt index 55a7b33..1caac5e 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ui/VpnRequestActivity.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ui/VpnRequestActivity.kt @@ -7,6 +7,7 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.net.VpnService +import android.os.Build.VERSION.SDK_INT import android.os.Bundle import android.widget.Toast import androidx.activity.result.contract.ActivityResultContract @@ -26,7 +27,15 @@ class VpnRequestActivity : AppCompatActivity() { super.onCreate(savedInstanceState) if (getSystemService()!!.isKeyguardLocked) { receiver = broadcastReceiver { _, _ -> connect.launch(null) } - registerReceiver(receiver, IntentFilter(Intent.ACTION_USER_PRESENT)) + if (SDK_INT >= 33) { + registerReceiver( + receiver, + IntentFilter(Intent.ACTION_USER_PRESENT), + Context.RECEIVER_NOT_EXPORTED + ) + } else { + registerReceiver(receiver, IntentFilter(Intent.ACTION_USER_PRESENT)) + } } else connect.launch(null) }