Can someone point me in the right direction when it comes to replacing this deprecated code for checking the internet connection on a device?
private val isNetworkAvailable = MutableStateFlow(false)
fun checkNetworkAvailability(context: Context): MutableStateFlow<Boolean> {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.registerDefaultNetworkCallback(this)
var isConnected = false
// allNetworks Deprecated
connectivityManager.allNetworks.forEach { network ->
val networkCapability = connectivityManager.getNetworkCapabilities(network)
networkCapability?.let {
if(it.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
isConnected = true
return@forEach
}
}
}
isNetworkAvailable.value = isConnected
return isNetworkAvailable
}
from Connectivity Manager - AllNetworks Deprecated
No comments:
Post a Comment