Androidはping操作でネットワーク検出を行い、かかった時間を返します
12254 ワード
/**
* author:
* date: 2017/11/7
* desc:ping
*/
public class PingNet {
private static final String TAG = "PingNet";
/**
* @param pingNetEntity
* @return
*/
public static PingNetEntity ping(PingNetEntity pingNetEntity) {
String line = null;
Process process = null;
BufferedReader successReader = null;
String command = "ping -c " + pingNetEntity.getPingCount() + " -w " + pingNetEntity.getPingWtime() + " " + pingNetEntity.getIp();
// String command = "ping -c " + pingCount + " " + host;
try {
process = Runtime.getRuntime().exec(command);
if (process == null) {
Log.e(TAG, "ping fail:process is null.");
append(pingNetEntity.getResultBuffer(), "ping fail:process is null.");
pingNetEntity.setPingTime(null);
pingNetEntity.setResult(false);
return pingNetEntity;
}
successReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = successReader.readLine()) != null) {
Log.i(TAG, line);
append(pingNetEntity.getResultBuffer(), line);
String time;
if ((time = getTime(line)) != null) {
pingNetEntity.setPingTime(time);
}
}
int status = process.waitFor();
if (status == 0) {
Log.i(TAG, "exec cmd success:" + command);
append(pingNetEntity.getResultBuffer(), "exec cmd success:" + command);
pingNetEntity.setResult(true);
} else {
Log.e(TAG, "exec cmd fail.");
append(pingNetEntity.getResultBuffer(), "exec cmd fail.");
pingNetEntity.setPingTime(null);
pingNetEntity.setResult(false);
}
Log.i(TAG, "exec finished.");
append(pingNetEntity.getResultBuffer(), "exec finished.");
} catch (IOException e) {
Log.e(TAG, String.valueOf(e));
} catch (InterruptedException e) {
Log.e(TAG, String.valueOf(e));
} finally {
Log.i(TAG, "ping exit.");
if (process != null) {
process.destroy();
}
if (successReader != null) {
try {
successReader.close();
} catch (IOException e) {
Log.e(TAG, String.valueOf(e));
}
}
}
Log.i(TAG, pingNetEntity.getResultBuffer().toString());
return pingNetEntity;
}
private static void append(StringBuffer stringBuffer, String text) {
if (stringBuffer != null) {
stringBuffer.append(text + "
");
}
}
private static String getTime(String line) {
String[] lines = line.split("
");
String time = null;
for (String l : lines) {
if (!l.contains("time="))
continue;
int index = l.indexOf("time=");
time = l.substring(index + "time=".length());
Log.i(TAG, time);
}
return time;
}
}
2.ping結果エンティティークラスは、ping操作後に得られた結果を保存するために使用され、必要に応じてフィールドを追加することができる
public class PingNetEntity {
/*
TODO: ping ip
*/
private String ip;
/*
TODO: ping
*/
private int pingCount;
/*
TODO:ping
*/
private int pingWtime;
/*
TODO: ping
*/
private StringBuffer resultBuffer;
/*
TODO:ping ip
*/
private String pingTime;
/*
TODO: ping
*/
private boolean result;
public PingNetEntity(String ip, int pingCount, int pingWtime,StringBuffer resultBuffer) {
this.ip = ip;
this.pingWtime=pingWtime;
this.pingCount = pingCount;
this.resultBuffer = resultBuffer;
}
public String getPingTime() {
return pingTime;
}
public void setPingTime(String pingTime) {
this.pingTime = pingTime;
}
public StringBuffer getResultBuffer() {
return resultBuffer;
}
public void setResultBuffer(StringBuffer resultBuffer) {
this.resultBuffer = resultBuffer;
}
public int getPingCount() {
return pingCount;
}
public void setPingCount(int pingCount) {
this.pingCount = pingCount;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public boolean isResult() {
return result;
}
public void setResult(boolean result) {
this.result = result;
}
public int getPingWtime() {
return pingWtime;
}
public void setPingWtime(int pingWtime) {
this.pingWtime = pingWtime;
}
}
3.Main(ここではツールクラスが使用可能かどうかをテストするため)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// , UI , ANR
new Thread(new Runnable() {
@Override
public void run() {
PingNetEntity pingNetEntity=new PingNetEntity("www.baidu.com",3,5,new StringBuffer());
pingNetEntity=PingNet.ping(pingNetEntity);
Log.i("testPing",pingNetEntity.getIp());
Log.i("testPing","time="+pingNetEntity.getPingTime());
Log.i("testPing",pingNetEntity.isResult()+"");
}
}).start();
}
}
4.注意点:ネットワーク操作を行う場合、Manifestに適切なネットワークアクセス権を追加してください.