androidandroid-connectivitymanager

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.


Solution

  • The solution is this:

    val capability = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
    return capability?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false