android如何检查wifi是否已连接但没有互联网连接 [英] android how to check wifi is connected but no internet connection
问题描述
android我的设备已连接wifi,但如何连接wifi却没有互联网连接
android my device connected with wifi but how to if wifi is connected but these is no internet connection
以下是我的代码,我试图检查是否没有互联网连接
following is my code that i trying to check if no internet connection
public static boolean isConnectedWifi(Context context) {
NetworkInfo info=null;
if(context!=null){
info= IsNetConnectionAvailable.getNetworkInfo(context);
}
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
当没有互联网访问时,它总是返回true
it always return true when no internet access
推荐答案
NetworInfo.isAvailable
和NetworkInfo.isConnected
仅指示网络连接是否可能或存在,而不能指示连接情况是否可以访问公共互联网,长话短说,他们无法告诉我们该设备确实在线.
NetworInfo.isAvailable
and NetworkInfo.isConnected
only indicate whether network connectivity is possible or existed, they can't indicate whether the connected situation has access to the public internet, long story short, they can't tell us the device is online indeed.
要检查设备是否在线,请尝试以下方法:
To check whether a device is online, try the following methods:
第一
@TargetApi(Build.VERSION_CODES.M)
public static boolean isNetworkOnline1(Context context) {
boolean isOnline = false;
try {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkCapabilities capabilities = manager.getNetworkCapabilities(manager.getActiveNetwork()); // need ACCESS_NETWORK_STATE permission
isOnline = capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
} catch (Exception e) {
e.printStackTrace();
}
return isOnline;
}
优势:1.可以在UI线程上运行; 2.快速准确.
Strength: 1. could run on UI thread; 2. fast and accurate.
弱点:需要API> = 23和兼容性问题.
Weakness: need API >= 23 and compatibility issues.
第二:
public static boolean isNetworkOnline2() {
boolean isOnline = false;
try {
Runtime runtime = Runtime.getRuntime();
Process p = runtime.exec("ping -c 1 8.8.8.8");
int waitFor = p.waitFor();
isOnline = waitFor == 0; // only when the waitFor value is zero, the network is online indeed
// BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
// String str;
// while ((str = br.readLine()) != null) {
// System.out.println(str); // you can get the ping detail info from Process.getInputStream()
// }
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return isOnline;
}
优势:1.可以在UI线程上运行; 2.您可以多次ping并统计最小/平均/最大延迟时间和丢包率.
Strength: 1. could run on UI thread; 2. you can ping many times and do statistics for min/avg/max delayed time and packet loss rate.
弱点:兼容性问题.
第三:
public static boolean isNetworkOnline3() {
boolean isOnline = false;
try {
URL url = new URL("http://www.google.com"); // or your server address
// URL url = new URL("http://www.baidu.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Connection", "close");
conn.setConnectTimeout(3000);
isOnline = conn.getResponseCode() == 200;
} catch (IOException e) {
e.printStackTrace();
}
return isOnline;
}
强度:可以在所有设备和API上使用.
Strength: could use on all devices and APIs.
弱点:耗时的操作,无法在UI线程上运行.
Weakness: time-consuming operation, can't run on UI thread.
第四名:
public static boolean isNetworkOnline4() {
boolean isOnline = false;
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress("8.8.8.8", 53), 3000);
// socket.connect(new InetSocketAddress("114.114.114.114", 53), 3000);
isOnline = true;
} catch (IOException e) {
e.printStackTrace();
}
return isOnline;
}
优势:1.可以在所有设备和API上使用; 2.相对快速和准确.
Strength: 1. could use on all devices and APIs; 2. relatively fast and accurate.
弱点:耗时的操作,无法在UI线程上运行.
Weakness: time-consuming operation, can't run on UI thread.
这篇关于android如何检查wifi是否已连接但没有互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!