gpt4 book ai didi

android - 检查Android互联网连接的完美功能,包括蓝牙锅

转载 作者:可可西里 更新时间:2023-11-01 19:01:00 24 4
gpt4 key购买 nike

我的应用程序在 wifi 和移动网络中运行良好,但在通过蓝牙网络共享连接时无法检测到。

public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();

if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}

我尝试运行其他几个应用程序。他们也没有显示网络连接,但谷歌应用程序运行完美,其他一些应用程序如 whatsap 也是如此。想知道他们是怎么做到的,以及为什么大多数应用程序都忽略了这一点。

任何人都可以告诉我一种方法来检查 android 中的互联网连接,通过所有可用的方式,包括蓝牙 pan 和代理等。

任何帮助将不胜感激。提前致谢..

最佳答案

尝试连接到“始终可用”的网站。如果存在任何连接,则应返回 true:

protected static boolean hasInternetAccess()
{
try
{
URL url = new URL("http://www.google.com");

HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application:1");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1000 * 30);
urlc.connect();

// http://www.w3.org/Protocols/HTTP/HTRESP.html
if (urlc.getResponseCode() == 200 || urlc.getResponseCode() > 400)
{
// Requested site is available
return true;
}
}
catch (Exception ex)
{
// Error while trying to connect
return false;
}
return false;
}

关于android - 检查Android互联网连接的完美功能,包括蓝牙锅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28059519/

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