您的位置:首页 > 移动开发 > Android开发

android ping的实现

2015-09-14 01:14 531 查看
注意要添加以下权限

<uses-permissionandroid:name="android.permission.INTERNET"/>


public static String pingHost(String str){
String resault="";
try {
// TODO: Hardcoded for now, make it UI configurable
Process p = Runtime.getRuntime().exec("/system/bin/ping -c 4 -w 100 " +str);
//Process p = Runtime.getRuntime().exec("ping 192.168.1.111");
int status = p.waitFor();

InputStream input = p.getInputStream();

//Log.i("info");

BufferedReader in = new BufferedReader(new InputStreamReader(input));

StringBuffer buffer = new StringBuffer();

String line = "";

while ((line = in.readLine()) != null) {
buffer.append(line);
Log.i("info", line);
}

if (status == 0) {
//  mTextView.setText("success") ;
resault="success";
Log.i("info", "success......");
}
else
{
resault="faild";
//  mTextView.setText("fail");
Log.i("info", "failed......" + status + "...");
}
} catch (IOException e) {
//  mTextView.setText("Fail: IOException"+"\n");
} catch (InterruptedException e) {
//  mTextView.setText("Fail: InterruptedException"+"\n");
}

return resault;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: