Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConnectivityManager.getActiveNetworkInfo() / NetworkInfo is deprecated in API 29. What's an alternative?

Seem like whole NetworkInfo is deprecated on API 29.

So I am looking for an alternative to check if the network is connected. E.g. alternative to

connectivityManager.activeNetworkInfo?.isConnected == true

Sidenote: I know there is a callback now, but I'd like to get this info synchronously. Also, of course I'm aware that it may not be a precise info though I'd like to have it.

like image 558
hrach Avatar asked Jul 11 '19 07:07

hrach


1 Answers

The solution is this:

val capability = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
return capability?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false
like image 111
hrach Avatar answered Sep 30 '22 08:09

hrach