Android ping网络是否连通
public class CheckWifiConnThread {
private String ip;
public CheckWifiConnThread(String ip){
if(ip.contains(":")){
this.ip = ip.substring(0,ip.indexOf(":"));
} else {
this.ip=ip;
}
}
public boolean runPing(){
String str=Ping(ip);
return str.equals("success");
}
private String Ping(String str) {
String result = "";
Process p;
try {
p = Runtime.getRuntime().exec("ping -c 1 -w 3 " + str);
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);
}
input.close();
in.close();
if(buffer.toString().indexOf("100%")!=-1||buffer.toString().equals("")){
result = "fail";
} else{
result = "success";
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}```