我要檢查WiFi或移動數據連接(可檢查)上,但能夠訪問互聯網或不如何檢查WiFi或數據連接上,但是互聯網訪問或不
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
public static boolean isInternetAccessible(Context context) {
if (isNetworkAvailable(context)) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://aapapps.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", "Couldn't check internet connection", e);
}
} else {
Log.d("LOG_TAG", "Internet not available!");
}
return false;
}
你的問題讓我困惑。你想檢查互聯網是否可用? –
[檢測Android上是否有可用的Internet連接]可能的重複(http://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android) –
我想檢查互聯網是否可以訪問,如果移動或數據連接在 –