'How can I check internet connection in Android Q?
Before Android Q all you had to do is use the NetworkUtils class. But this class was deprecated in API 29. I've been searching for an alternative, and I couldn't seem to find one.
So, how can I check the internet connection in Android Q?
Solution 1:[1]
Use this code snippet.
@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;
}
Happy Coding :)
Solution 2:[2]
If you are looking for a simple code, you can use this:
public static boolean isInternetConnected(Context getApplicationContext) {
boolean status = false;
ConnectivityManager cm = (ConnectivityManager) getApplicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (cm.getActiveNetwork() != null && cm.getNetworkCapabilities(cm.getActiveNetwork()) != null) {
// connected to the internet
status = true;
}
} else {
if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
// connected to the internet
status = true;
}
}
}
return status;
}
Solution 3:[3]
The ConnectivityManager only checks whether the smartphone could theoretically establish an Internet connection. Whether an Internet connection actually exists, e.g. if the network quality is very poor, can only be determined with a ping or a web address.
Solution 4:[4]
Just use this code and call isInternatAvailable(context).
private static final String CMD_PING_GOOGLE = "ping -c 1 google.com";
public static boolean isInternetAvailable(@NonNull Context context) {
return isConnected(context) && checkInternetPingGoogle();
}
public static boolean isConnected(@NonNull Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm != null) {
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
} else {
return false;
}
}
public static boolean checkInternetPingGoogle(){
try {
int a = Runtime.getRuntime().exec(CMD_PING_GOOGLE).waitFor();
return a == 0x0;
} catch (IOException ioE){
EMaxLogger.onException(TAG, ioE);
} catch (InterruptedException iE){
EMaxLogger.onException(TAG, iE);
}
return false;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | guenter47 |
Solution 4 |