|
|
@ -23,15 +23,22 @@ class NetworkChangedListener(private val context: Context) { |
|
|
|
return@lazy null |
|
|
|
return@lazy null |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun register() { |
|
|
|
private val networkCallback: ConnectivityManager.NetworkCallback? by lazy { |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
|
|
|
connectivityManager.registerDefaultNetworkCallback( |
|
|
|
return@lazy object : ConnectivityManager.NetworkCallback() { |
|
|
|
object : ConnectivityManager.NetworkCallback() { |
|
|
|
|
|
|
|
override fun onAvailable(network: Network) { |
|
|
|
override fun onAvailable(network: Network) { |
|
|
|
onNetworkChanged?.invoke() |
|
|
|
onNetworkChanged?.invoke() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
return@lazy null |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun register() { |
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
|
|
|
|
|
|
|
networkCallback?.let { |
|
|
|
|
|
|
|
connectivityManager.registerDefaultNetworkCallback(it) |
|
|
|
|
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
receiver?.let { |
|
|
|
receiver?.let { |
|
|
|
context.registerReceiver(it, it.filter) |
|
|
|
context.registerReceiver(it, it.filter) |
|
|
@ -40,10 +47,16 @@ class NetworkChangedListener(private val context: Context) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun unRegister() { |
|
|
|
fun unRegister() { |
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
|
|
|
|
|
|
|
networkCallback?.let { |
|
|
|
|
|
|
|
connectivityManager.unregisterNetworkCallback(it) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
receiver?.let { |
|
|
|
receiver?.let { |
|
|
|
context.unregisterReceiver(it) |
|
|
|
context.unregisterReceiver(it) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
inner class NetworkChangedReceiver : BroadcastReceiver() { |
|
|
|
inner class NetworkChangedReceiver : BroadcastReceiver() { |
|
|
|
|
|
|
|
|
|
|
|