该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
下面是代码
package com.example.network;
import java.io.IOException;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public class PingIp {
public static boolean startPing(String ip, Context activity){
Log.e("Ping", "startPing...");
boolean success=false;
Process p =null;
Context context=activity.getApplicationContext();
try {
p = Runtime.getRuntime().exec("/system/bin/ping -c 1 -i 0.2 -W 1 " +ip);
int status = p.waitFor();
if (status == 0) {
success=true;
Log.e("Ping", "chenggong");
Toast.makeText(context,"连接成功",Toast.LENGTH_LONG).show();
} else {
success=false;
Log.e("Ping", "shibai");
Toast.makeText(context,"连接不到服务器",Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
success=false;
} catch (InterruptedException e) {
success=false;
}finally{
p.destroy();
}
return success;
}
}