Android:Ping命令测试网络

发表于:2014-3-11 11:00

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:VanishMagic    来源:51Testing软件测试网采编

分享:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new NetPing().execute();
}
public String Ping(String str) {
String resault = "";
Process p;
try {
//ping -c 3 -w 100  中  ,-c 是指ping的次数 3是指ping 3次 ,-w 100  以秒为单位指定超时间隔,是指超时时间为100秒
p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + str);
int status = p.waitFor();
InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null){
buffer.append(line);
}
System.out.println("Return ============" + buffer.toString());
if (status == 0) {
resault = "success";
} else {
resault = "faild";
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return resault;
}
private class NetPing extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String s = "";
s = Ping("www.baidu.com");
Log.i("ping", s);
return s;
}
}
}
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号