我要检查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) –
我想检查互联网是否可以访问,如果移动或数据连接在 –