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 9585cff5d..151d5c38e 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 b1f8deb30..146c6e859 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 aab069639..6a278b892 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 1177633a3..abf71e65f 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, "") }