Ping Application in Android

后端 未结 3 498
一生所求
一生所求 2020-11-30 01:44

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 ha

3条回答
  •  伪装坚强ぢ
    2020-11-30 01:49

    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.

提交回复
热议问题