1.isNetworkConnected()
private boolean isNetworkConnected() {
final ConnectivityManager connManager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo netInfo = connManager.getActiveNetworkInfo();
if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
return false;
}
2.ping这种方式我在adb shell ping www.baidu.com是可以的。但是在测试demo里面不知道是不是权限问题不行。因为我是在framework上用的。编译了版本发现是可以的。如果有朋友知道为什么单独写个demo不行的原因求告知
public static boolean pingHostnamePass(String host) {
boolean pass = false;
if (host == null) {
return false;
}
try {
// TODO: Hardcoded for now, make it UI configurable
pass = false;
String command = "ping -c 5 -w 10 " + host;
Process p = Runtime.getRuntime().exec(command);
int status = p.waitFor();
if (status == 0) {
pass = true;
} else {
pass = false;
}
} catch (UnknownHostException e) {
Log.e(TAG, "UnknownHostException: " + e.toString());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.toString());
} catch (InterruptedException e) {
Log.e(TAG, "InterruptedException: " + e.toString());
}
Log.d(TAG, "====> Access " + host + " pass? " + pass);
return pass;
}