Android examples for Android OS:Shell Command
ping Command
//package com.java2s; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static String ping(String url, int count) { String str = ""; try {/*from w w w. j a v a 2 s. co m*/ Process process = Runtime.getRuntime().exec( "ping -c " + count + " " + 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 = parseIpFromPing(output.toString()); // Log.d(TAG, str); } catch (IOException e) { // body.append("Error\n"); e.printStackTrace(); } return str; } private static String parseIpFromPing(String ping) { String ip = null; try { if (ping.contains("ping")) { int indexOpen = ping.indexOf("("); int indexClose = ping.indexOf(")"); ip = ping.substring(indexOpen + 1, indexClose); } } catch (Exception e) { e.printStackTrace(); ip = ping; } return ip; } }