3

I'm trying to ping a site to detect wether I have internet or not in my android app. On many devices this does work well, but today, with a new devices, it allways returns that I have no internet.

This is the code:

    static public  boolean isInternetAvailable(String url){
    Runtime runtime = Runtime.getRuntime();
    try {
        String command = "/system/bin/ping -c 1 " + url;
        Process ipProcess = runtime.exec(command);
        int     exitValue = ipProcess.waitFor();
        return (exitValue == 0);

    } catch (IOException e)          { e.printStackTrace(); }
    catch (InterruptedException e) { e.printStackTrace(); }

    return false;
}

Even though the server is online, and I can ping it from various other devices, on the new device it allways returns false. Could it be possible that the ping command is blocked for some reason? The new device is a Samsung GT-19305 with Android version 4.3.

Supersonic
  • 430
  • 1
  • 11
  • 35
Tristus
  • 183
  • 1
  • 2
  • 15
  • 2
    Are you able to do anything else that requires internet connection? Please note that in android you need to use a different thread than the one that the View is working on. – Mihai Mar 04 '15 at 10:46
  • yes, the device has internet, we can use the browser to search the web and do anything we want. – Tristus Mar 04 '15 at 10:48
  • Can only confirm that on one of my devices the ping does not work too. – greenapps Mar 04 '15 at 11:22
  • I've found the answer [here](http://stackoverflow.com/questions/9570237/android-check-internet-connection) helpful. – juunas Mar 04 '15 at 11:40
  • What is helpfull there? There is no solution there for the ping problem here. – greenapps Mar 04 '15 at 12:25
  • Ok, we found out that exitvalue returns 2 meening the sever get the ping but does not response. – Tristus Mar 06 '15 at 15:59

1 Answers1

0

You could try this:

Process process = new ProcessBuilder()
       .command("/system/bin/ping", "android.com")
       .redirectErrorStream(true)
       .start();
   try {
     InputStream in = process.getInputStream();
     OutputStream out = process.getOutputStream();

     readStream(in);

    finally {
     process.destroy();
   }
 }
Clyde D'Cruz
  • 1,915
  • 1
  • 14
  • 36