1

I have a code to check if a user is connected

val connected:Boolean
        val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        connected = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_VPN).state == NetworkInfo.State.CONNECTED

if(connected){
   
} else {
}

but every time I build it tells me the its depreciated.

If there another way to check a user connectivity change please share.

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
  • 1
    [see this link](https://stackoverflow.com/questions/32547006/connectivitymanager-getnetworkinfoint-deprecated) – user3170251 Feb 08 '19 at 16:37

1 Answers1

0

From the Android documentation:

This method was deprecated in API level 23. This method does not support multiple connected networks of the same type. Use getAllNetworks() and getNetworkInfo(android.net.Network) instead.

In your case you probably actually want:

Network network = connectivityManager.getActiveNetwork();
NetworkCapabilities caps = connectivityManager.getNetworkCapabilities(network);
connected = caps.hasCapability(TRANSPORT_VPN) && network.isConnected();
Philip Whitehouse
  • 4,293
  • 3
  • 23
  • 36