인터넷 연결을 확인하는 빠르고 효과적인 방법을 찾고 있습니다. 인터넷 연결 상태를 찾기 위해 ping을 수행하는 것이 좋습니다.하지만 구글을 핑 (ping)하는 여러 가지 방법을 찾았고 모든 것을 사용하는 것이 혼란 스럽습니다. 아래에 제가 본 방법이 있습니다.Android : 활성 인터넷 연결을 확인하는 가장 효과적인 방법은 무엇입니까
방법 1 :
public boolean isOnline() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
방법 2 :
public Boolean isOnline() {
try {
Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
int returnVal = p1.waitFor();
boolean reachable = (returnVal==0);
return reachable;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
방법 3 :
public static boolean hasInternetAccess(Context context) {
if (isNetworkAvailable(context)) {
try {
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 204 &&
urlc.getContentLength() == 0);
} catch (IOException e) {
Log.e(TAG, "Error checking internet connection", e);
}
} else {
Log.d(TAG, "No network available!");
}
return false;
}
방법 4 :
public static boolean hasActiveInternetConnection(Context context) {
if (isNetworkAvailable(context)) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
Log.e(LOG_TAG, "Error checking internet connection", e);
}
} else {
Log.d(LOG_TAG, "No network available!");
}
return false;
}
어느 하나를 선택해야합니까 ?? 나는 더 빠르고 효과적인 방법이 필요하다.
가능한 복제 [안드로이드에서 인터넷 연결을 확인 (네트워크하지 연결)] (http://stackoverflow.com/questions/41545504/check-internet-connection-in-android-not-network-connection) – W4R10CK
참조 [Detect Internet Connection Status] (http://www.androidhive.info/2012/07/android-detect-internet-connection-status/)에 대한이 링크 –
방법 1은 그 필요성을 우회하기 때문에 방법 1이라고 말하고 싶습니다. 기능적인 DNS 서버. 그러나 방법 1과 2는 그다지 다르지 않습니다. –