Reputation: 895
I try to check the status of my network (connected or disconnected) using this function:
// Check Network status
private fun isNetworkAvailable(): Boolean {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE)
return if (connectivityManager is ConnectivityManager) {
val networkInfo = connectivityManager.activeNetworkInfo
networkInfo.isConnected
}
else false
}
This gives me a java.lang.IllegalStateException: networkInfo must not be null - error when run with a disconnected network. Why? And how can I solve this?
Upvotes: 4
Views: 17344
Reputation: 10619
getActiveNetwork
has been deprecated in API 29, so this is the best solution:
fun isInternetAvailable(context: Context): Boolean {
var result = false
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connectivityManager.activeNetwork ?: return false
val actNw =
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
result = when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
} else {
connectivityManager.activeNetworkInfo?.run {
result = when (type) {
ConnectivityManager.TYPE_WIFI -> true
ConnectivityManager.TYPE_MOBILE -> true
ConnectivityManager.TYPE_ETHERNET -> true
else -> false
}
}
}
return result
}
Upvotes: 6
Reputation: 303
Use this code, it will surely help
fun checkConnectivity(context: Context): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = connectivityManager.activeNetworkInfo
if(activeNetwork?.isConnected!=null){
return activeNetwork.isConnected
}
else{
return false
}
}
Upvotes: 3
Reputation: 9558
One of the easiest way is, Add an extension to Context class like below,
val Context.isConnected: Boolean
get() {
return (getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
.activeNetworkInfo?.isConnected == true
}
Now you can use it in activity like,
if (!isConnected) {
// connection is not available...
}
You can use this way in activity, fragment or anywhere within the context.
Upvotes: 1
Reputation: 12005
According to the docs activeNetworkInfo
might be null:
Returns details about the currently active default data network. When connected, this network is the default route for outgoing connections. You should always check isConnected() before initiating network traffic. This may return null when there is no default network.
To make sure it doesn't crash, just use this:
private fun isNetworkAvailable(): Boolean {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE)
return if (connectivityManager is ConnectivityManager) {
val networkInfo: NetworkInfo? = connectivityManager.activeNetworkInfo
networkInfo?.isConnected ?: false
} else false
}
Upvotes: 17