Skip to content

Commit f91ddb7

Browse files
Fix ForegroundServiceStartNotAllowedException: switch server without restarting service
1 parent ac81bb5 commit f91ddb7

6 files changed

Lines changed: 29 additions & 2 deletions

File tree

V2rayNG/app/src/main/java/com/kiktor/v2whitelist/AppConfig.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ object AppConfig {
148148
const val MSG_STATE_STOP = 4
149149
const val MSG_STATE_STOP_SUCCESS = 41
150150
const val MSG_STATE_RESTART = 5
151+
const val MSG_STATE_SWITCH_SERVER = 51
151152
const val MSG_MEASURE_DELAY = 6
152153
const val MSG_MEASURE_DELAY_SUCCESS = 61
153154
const val MSG_MEASURE_CONFIG = 7

V2rayNG/app/src/main/java/com/kiktor/v2whitelist/contracts/ServiceControl.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ interface ServiceControl {
2525
* @return True if the socket is protected, false otherwise.
2626
*/
2727
fun vpnProtect(socket: Int): Boolean
28+
29+
/**
30+
* Gets the VPN interface.
31+
* @return The VPN interface.
32+
*/
33+
fun getVpnInterface(): ParcelFileDescriptor?
2834
}

V2rayNG/app/src/main/java/com/kiktor/v2whitelist/handler/SmartConnectManager.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,17 @@ object SmartConnectManager {
267267
}
268268

269269
if (nextBest != null) {
270-
V2RayServiceManager.stopVService(context)
271270
Log.i(AppConfig.TAG, "Switching to next best server: ${nextBest.second.remarks}")
272271
sendStatus(context, context.getString(R.string.status_found_best))
273272
delay(500)
274273
sendStatus(context, context.getString(R.string.status_connecting_to, nextBest.second.remarks))
275274
MmkvManager.setSelectServer(nextBest.first)
276-
V2RayServiceManager.startVService(context)
275+
276+
if (V2RayServiceManager.isRunning()) {
277+
MessageUtil.sendMsg2Service(context, AppConfig.MSG_STATE_SWITCH_SERVER, "")
278+
} else {
279+
V2RayServiceManager.startVService(context)
280+
}
277281
startFailoverTimer(context)
278282
}
279283
}

V2rayNG/app/src/main/java/com/kiktor/v2whitelist/handler/V2RayServiceManager.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ object V2RayServiceManager {
354354
startVService(serviceControl.getService())
355355
}
356356

357+
AppConfig.MSG_STATE_SWITCH_SERVER -> {
358+
Log.i(AppConfig.TAG, "Switching Server inside Service")
359+
val vpnInterface = serviceControl.getVpnInterface()
360+
stopCoreLoop()
361+
Thread.sleep(1000L) // Wait for core to fully release resources
362+
startCoreLoop(vpnInterface)
363+
}
364+
357365
AppConfig.MSG_MEASURE_DELAY -> {
358366
measureV2rayDelay()
359367
}

V2rayNG/app/src/main/java/com/kiktor/v2whitelist/service/V2RayProxyOnlyService.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class V2RayProxyOnlyService : Service(), ServiceControl {
7272
return true
7373
}
7474

75+
override fun getVpnInterface(): ParcelFileDescriptor? {
76+
return null
77+
}
78+
7579
/**
7680
* Binds the service.
7781
* @param intent The intent.

V2rayNG/app/src/main/java/com/kiktor/v2whitelist/service/V2RayVpnService.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ class V2RayVpnService : VpnService(), ServiceControl {
8181
stopAllService()
8282
}
8383

84+
override fun getVpnInterface(): ParcelFileDescriptor? {
85+
return mInterface
86+
}
87+
8488
// override fun onLowMemory() {
8589
// stopV2Ray()
8690
// super.onLowMemory()

0 commit comments

Comments
 (0)