Ping Application in Android

user1944616 picture user1944616 · Jan 29, 2013 · Viewed 26.7k times · Source

I am making an application which will implement some features of the "ping" command.The problem is, I have no idea of which library/libraries to use in ANDROID. anyone have any idea for it?

I have visited these stackoverflow links but they weren't very helpful.

Answer

Sahil Mahajan Mj picture Sahil Mahajan Mj · Jan 29, 2013

I have used following code to ping.

public String ping(String url) {
    String str = "";
    try {
        Process process = Runtime.getRuntime().exec(
                "/system/bin/ping -c 8 " + url);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                process.getInputStream()));
        int i;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((i = reader.read(buffer)) > 0)
            output.append(buffer, 0, i);
        reader.close();

        // body.append(output.toString()+"\n");
        str = output.toString();
        // Log.d(TAG, str);
    } catch (IOException e) {
        // body.append("Error\n");
        e.printStackTrace();
    }
    return str;
}

Here in the url, you need to pass the address, on which you want to ping.