I was using below method for checking weather user is connected to internet or not.
public boolean internetIsConnected() {
try {
String command = "ping -c 1 google.com";
return (Runtime.getRuntime().exec(command).waitFor() == 0);
} catch (Exception e) {
return false;
}
}
Manifest is as below
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
but after I switched to latest API 30. It just freezes the app and returns false. any suggestion to get this working.
Thank you.
Edit : As per comment's suggestions I tried below similar method but still Not working.
public boolean internetIsConnected() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
System.out.println(" mExitValue "+exitValue);
return (exitValue == 0);
} catch (IOException e){
System.out.println(" IO Error ");
e.printStackTrace(); }
catch (InterruptedException e) {
System.out.println(" Interrupted Error ");
e.printStackTrace(); }
return false;
}
I got :
I/System.out: mExitValue 1
So it is failing in Try block itself. I tried finding what Exit value "1" means but it only shows for "0" Here This Happens in both Emulator as well as physical device. Any suggestion is appreciated.
Thank you