mirror of
https://github.com/gedoor/legado.git
synced 2024-09-03 09:55:00 +08:00
web服务监测网络变化,及时更新IP
This commit is contained in:
parent
935fa500f0
commit
d8945e5348
@ -0,0 +1,61 @@
|
||||
package io.legado.app.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.Network
|
||||
import android.os.Build
|
||||
import splitties.systemservices.connectivityManager
|
||||
|
||||
/**
|
||||
* 监测网络变化
|
||||
*/
|
||||
class NetworkChangedListener(private val context: Context) {
|
||||
|
||||
var onNetworkChanged: (() -> Unit)? = null
|
||||
|
||||
private val receiver: NetworkChangedReceiver? by lazy {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
||||
NetworkChangedReceiver()
|
||||
}
|
||||
return@lazy null
|
||||
}
|
||||
|
||||
fun register() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
connectivityManager.registerDefaultNetworkCallback(
|
||||
object : ConnectivityManager.NetworkCallback() {
|
||||
override fun onAvailable(network: Network) {
|
||||
onNetworkChanged?.invoke()
|
||||
}
|
||||
}
|
||||
)
|
||||
} else {
|
||||
receiver?.let {
|
||||
context.registerReceiver(it, it.filter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun unRegister() {
|
||||
receiver?.let {
|
||||
context.unregisterReceiver(it)
|
||||
}
|
||||
}
|
||||
|
||||
inner class NetworkChangedReceiver : BroadcastReceiver() {
|
||||
|
||||
val filter = IntentFilter().apply {
|
||||
@Suppress("DEPRECATION")
|
||||
addAction(ConnectivityManager.CONNECTIVITY_ACTION)
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
onNetworkChanged?.invoke()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package io.legado.app.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.ConnectivityManager
|
||||
|
||||
/**
|
||||
* 监测网络变化
|
||||
*/
|
||||
class NetworkChangedReceiver : BroadcastReceiver() {
|
||||
|
||||
var onReceiver: ((context: Context, intent: Intent) -> Unit)? = null
|
||||
|
||||
val filter = IntentFilter().apply {
|
||||
@Suppress("DEPRECATION")
|
||||
addAction(ConnectivityManager.CONNECTIVITY_ACTION)
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
onReceiver?.invoke(context, intent)
|
||||
}
|
||||
|
||||
}
|
@ -10,7 +10,7 @@ import io.legado.app.constant.AppConst
|
||||
import io.legado.app.constant.EventBus
|
||||
import io.legado.app.constant.IntentAction
|
||||
import io.legado.app.constant.PreferKey
|
||||
import io.legado.app.receiver.NetworkChangedReceiver
|
||||
import io.legado.app.receiver.NetworkChangedListener
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.web.HttpServer
|
||||
import io.legado.app.web.WebSocketServer
|
||||
@ -36,7 +36,9 @@ class WebService : BaseService() {
|
||||
private var httpServer: HttpServer? = null
|
||||
private var webSocketServer: WebSocketServer? = null
|
||||
private var notificationContent = ""
|
||||
private val networkChangedReceiver = NetworkChangedReceiver()
|
||||
private val networkChangedListener by lazy {
|
||||
NetworkChangedListener(this)
|
||||
}
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
@ -44,8 +46,8 @@ class WebService : BaseService() {
|
||||
notificationContent = getString(R.string.service_starting)
|
||||
upNotification()
|
||||
upTile(true)
|
||||
registerReceiver(networkChangedReceiver, networkChangedReceiver.filter)
|
||||
networkChangedReceiver.onReceiver = { _, _ ->
|
||||
networkChangedListener.register()
|
||||
networkChangedListener.onNetworkChanged = {
|
||||
upWebServer()
|
||||
}
|
||||
}
|
||||
@ -61,7 +63,7 @@ class WebService : BaseService() {
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
unregisterReceiver(networkChangedReceiver)
|
||||
networkChangedListener.unRegister()
|
||||
isRun = false
|
||||
if (httpServer?.isAlive == true) {
|
||||
httpServer?.stop()
|
||||
|
Loading…
Reference in New Issue
Block a user