mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
web服务监测网络变化,及时更新IP
This commit is contained in:
parent
c0ba24b6aa
commit
34a371fdc2
@ -23,15 +23,22 @@ class NetworkChangedListener(private val context: Context) {
|
||||
return@lazy null
|
||||
}
|
||||
|
||||
private val networkCallback: ConnectivityManager.NetworkCallback? by lazy {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
return@lazy object : ConnectivityManager.NetworkCallback() {
|
||||
override fun onAvailable(network: Network) {
|
||||
onNetworkChanged?.invoke()
|
||||
}
|
||||
}
|
||||
}
|
||||
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()
|
||||
}
|
||||
}
|
||||
)
|
||||
networkCallback?.let {
|
||||
connectivityManager.registerDefaultNetworkCallback(it)
|
||||
}
|
||||
} else {
|
||||
receiver?.let {
|
||||
context.registerReceiver(it, it.filter)
|
||||
@ -40,8 +47,14 @@ class NetworkChangedListener(private val context: Context) {
|
||||
}
|
||||
|
||||
fun unRegister() {
|
||||
receiver?.let {
|
||||
context.unregisterReceiver(it)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
networkCallback?.let {
|
||||
connectivityManager.unregisterNetworkCallback(it)
|
||||
}
|
||||
} else {
|
||||
receiver?.let {
|
||||
context.unregisterReceiver(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user