ping an IP address - Android Network

Android examples for Network:Ping

Description

ping an IP address

Demo Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.util.Log;

public class Main {
  private static final String tag = "";

  public static String ping(String ip, String count) {
    String result = "";
    try {//from  w w  w  . j a va  2s  .c  o m
      Process p = Runtime.getRuntime().exec("/system/bin/ping -c " + count + " -w 4 " + ip);
      if (p == null) {
        return result;
      }

      BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        if (line.contains("bytes from")) {
          Log.d(tag, "ping result = " + line);
          result += line + "\n";
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

    return result;

  }
}

Related Tutorials