ConnectivityManager getNetworkInfo(int) deprecated
You can use:
getActiveNetworkInfo();
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeNetwork = cm.getActiveNetworkInfo();if (activeNetwork != null) { // connected to the internet if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { // connected to wifi } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { // connected to mobile data }} else { // not connected to the internet}
Or in a switch case
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeNetwork = cm.getActiveNetworkInfo();if (activeNetwork != null) { // connected to the internet switch (activeNetwork.getType()) { case ConnectivityManager.TYPE_WIFI: // connected to wifi break; case ConnectivityManager.TYPE_MOBILE: // connected to mobile data break; default: break; }} else { // not connected to the internet}
February 2020 Update:
The accepted answer is deprecated again in 28 (Android P)
, but its replacement method only works on 23 (Android M)
. To support older devices, I wrote a helper function in both Kotlin and Java.
How to use:
int type = getConnectionType(getApplicationContext());
It returns an int
, you can change it to enum
in your code:
0: No Internet available (maybe on airplane mode, or in the process of joining an wi-fi).
1: Cellular (mobile data, 3G/4G/LTE whatever).
2: Wi-fi.
3: VPN
You can copy either the Kotlin or the Java version of the helper function.
Kotlin:
@IntRange(from = 0, to = 3)fun getConnectionType(context: Context): Int { var result = 0 // Returns connection type. 0: none; 1: mobile data; 2: wifi val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { cm?.run { cm.getNetworkCapabilities(cm.activeNetwork)?.run { if (hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { result = 2 } else if (hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { result = 1 } else if (hasTransport(NetworkCapabilities.TRANSPORT_VPN)){ result = 3 } } } } else { cm?.run { cm.activeNetworkInfo?.run { if (type == ConnectivityManager.TYPE_WIFI) { result = 2 } else if (type == ConnectivityManager.TYPE_MOBILE) { result = 1 } else if(type == ConnectivityManager.TYPE_VPN) { result = 3 } } } } return result}
Java:
@IntRange(from = 0, to = 3)public static int getConnectionType(Context context) { int result = 0; // Returns connection type. 0: none; 1: mobile data; 2: wifi ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (cm != null) { NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork()); if (capabilities != null) { if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { result = 2; } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { result = 1; } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) { result = 3; } } } } else { if (cm != null) { NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (activeNetwork != null) { // connected to the internet if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { result = 2; } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { result = 1; } else if (activeNetwork.getType() == ConnectivityManager.TYPE_VPN) { result = 3; } } } } return result;}
As for October 2018, accepted answer is deprecated.
getType()
, and types themselves, are now deprecated in API Level 28. From Javadoc:
Callers should switch to checking NetworkCapabilities#hasTransport instead with one of the NetworkCapabilities#TRANSPORT* constants
In order to use NetworkCapabilities
, you need to pass a Network
instance to the getNetworkCapabilities()
method. To get that instance you need to call getActiveNetwork()
which was added in API Level 23.
So I believe for now the right way to safely check whether you are connected to Wi-Fi or cellular network is:
public static boolean isNetworkConnected() { final ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm != null) { if (Build.VERSION.SDK_INT < 23) { final NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni != null) { return (ni.isConnected() && (ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_MOBILE)); } } else { final Network n = cm.getActiveNetwork(); if (n != null) { final NetworkCapabilities nc = cm.getNetworkCapabilities(n); return (nc.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)); } } } return false; }
You can also check for other types of TRANSPORT
, which you can find here.
Important note: if you are connected to Wi-Fi and to a VPN, then your current state could be TRANSPORT_VPN
, so you might want to also check for it.
Don't forget to add the following permission to your AndroidManifest file:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />