gpt4 book ai didi

android如何检查wifi已连接但没有互联网连接

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:26 28 4
gpt4 key购买 nike

android 我的设备已连接到 wifi,但如果已连接 wifi 但没有互联网连接怎么办

以下是我尝试检查是否没有互联网连接的代码

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

最佳答案

NetworInfo.isAvailableNetworkInfo.isConnected 仅表示网络连接是否可能或存在,它们不能表示连接的情况是否可以访问公共(public)互联网,长话短说,他们无法告诉我们设备确实在线。

检查设备是否在线,可尝试以下方法:

首先:

@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. 快速准确。

弱点:需要 API >= 23 和兼容性问题。

第二:

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并统计min/avg/max延迟时间和丢包率。

弱点:compatibility issues .

第三:

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 上使用。

缺点:操作耗时,不能在UI线程上运行。

第四:

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. 相对快速准确。

缺点:操作耗时,不能在UI线程上运行。

关于android如何检查wifi已连接但没有互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32394555/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com