I have created an app that pings my server when the app starts to check if it's online (this happens in a new thread) but it always says that my server is offline. The same happens with google.com and 8.8.8.8,.. I don't know what I am doing wrong.
This is my code:
System.out.println(executeCmd("ping -c 10 -w 1 vanlooverenkoen.be", false));
public static String executeCmd(String cmd, boolean sudo) {
try {
Process p;
if (!sudo)
p = Runtime.getRuntime().exec(cmd);
else {
p = Runtime.getRuntime().exec(new String[]{"su", "-c", cmd});
}
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s;
String res = "";
while ((s = stdInput.readLine()) != null) {
res += s + "\n";
}
p.destroy();
return res;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
this was another try: Didn't work out:
Process mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int mExitValue = mIpAddrProcess.waitFor();
System.out.println(" mExitValue " + mExitValue);
if (mExitValue == 0) {
System.out.println("0");
Snackbar.make(mLoginFormView, "Er kan verbonden worden met de server", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
} else {
System.out.println("else");
Snackbar.make(mLoginFormView, "Er kan geen verbinding gemaakt worden tussen uw appraat en de server", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}