From 28a90baf887a7ae8c0970c01631d0a31968f495d Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 6 Nov 2024 11:22:01 +0800 Subject: [PATCH] Bug fix https://github.com/2dust/v2rayNG/issues/3883 --- .../kotlin/com/v2ray/ang/service/QSTileService.kt | 10 +++------- .../com/v2ray/ang/service/V2RayServiceManager.kt | 12 +----------- .../src/main/kotlin/com/v2ray/ang/util/Utils.kt | 7 +++++++ .../com/v2ray/ang/viewmodel/MainViewModel.kt | 14 ++------------ 4 files changed, 13 insertions(+), 30 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/QSTileService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/QSTileService.kt index 9585cff5..151d5c38 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/QSTileService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/QSTileService.kt @@ -40,15 +40,11 @@ class QSTileService : TileService() { override fun onStartListening() { super.onStartListening() + setState(Tile.STATE_INACTIVE) mMsgReceive = ReceiveMessageHandler(this) val mFilter = IntentFilter(AppConfig.BROADCAST_ACTION_ACTIVITY) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - ContextCompat.registerReceiver(applicationContext, mMsgReceive, mFilter, ContextCompat.RECEIVER_EXPORTED) - } else { - ContextCompat.registerReceiver(applicationContext, mMsgReceive, mFilter, ContextCompat.RECEIVER_NOT_EXPORTED) - } - + ContextCompat.registerReceiver(applicationContext, mMsgReceive, mFilter, Utils.receiverFlags()) MessageUtil.sendMsg2Service(this, AppConfig.MSG_REGISTER_CLIENT, "") } @@ -56,7 +52,7 @@ class QSTileService : TileService() { super.onStopListening() try { - unregisterReceiver(mMsgReceive) + applicationContext.unregisterReceiver(mMsgReceive) mMsgReceive = null } catch (e: Exception) { e.printStackTrace() diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt index b1f8deb3..146c6e85 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayServiceManager.kt @@ -148,17 +148,7 @@ object V2RayServiceManager { mFilter.addAction(Intent.ACTION_SCREEN_ON) mFilter.addAction(Intent.ACTION_SCREEN_OFF) mFilter.addAction(Intent.ACTION_USER_PRESENT) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - ContextCompat.registerReceiver( - service, mMsgReceive, mFilter, - ContextCompat.RECEIVER_EXPORTED - ) - } else { - ContextCompat.registerReceiver( - service, mMsgReceive, mFilter, - ContextCompat.RECEIVER_NOT_EXPORTED - ) - } + ContextCompat.registerReceiver(service, mMsgReceive, mFilter, Utils.receiverFlags()) } catch (e: Exception) { Log.d(ANG_PACKAGE, e.toString()) } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt index aab06963..6a278b89 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt @@ -17,6 +17,7 @@ import android.util.Log import android.util.Patterns import android.webkit.URLUtil import androidx.appcompat.app.AppCompatDelegate +import androidx.core.content.ContextCompat import com.v2ray.ang.AppConfig import com.v2ray.ang.AppConfig.ANG_PACKAGE import com.v2ray.ang.AppConfig.LOOPBACK @@ -482,5 +483,11 @@ object Utils { return false } + fun receiverFlags(): Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + ContextCompat.RECEIVER_EXPORTED + } else { + ContextCompat.RECEIVER_NOT_EXPORTED + } + } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt index 1177633a..abf71e65 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt @@ -26,6 +26,7 @@ import com.v2ray.ang.handler.MmkvManager import com.v2ray.ang.handler.SettingsManager import com.v2ray.ang.util.MessageUtil import com.v2ray.ang.util.SpeedtestUtil +import com.v2ray.ang.util.Utils import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -53,18 +54,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { fun startListenBroadcast() { isRunning.value = false val mFilter = IntentFilter(AppConfig.BROADCAST_ACTION_ACTIVITY) - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - ContextCompat.registerReceiver( - getApplication(), mMsgReceiver, mFilter, - ContextCompat.RECEIVER_EXPORTED - ) - } else { - ContextCompat.registerReceiver( - getApplication(), mMsgReceiver, mFilter, - ContextCompat.RECEIVER_NOT_EXPORTED - ) - } + ContextCompat.registerReceiver(getApplication(), mMsgReceiver, mFilter, Utils.receiverFlags()) MessageUtil.sendMsg2Service(getApplication(), AppConfig.MSG_REGISTER_CLIENT, "") }