gpt4 book ai didi

android - 如何检查本地 wifi 上的互联网是否可用?

转载 作者:行者123 更新时间:2023-11-29 16:17:46 26 4
gpt4 key购买 nike

我需要在我的应用程序上检查本地 wifi 上的互联网是否可用,我试过了

requestRouteToHost 但它总是返回 false ,所以请帮助

最佳答案

这是测试设备是否有 INTERNET 连接时间的最佳方法。

public static boolean hasActiveInternetConnection(Context context) {
if (isNetworkAvailable(context)) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.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, "Error checking internet connection", e);
}
} else {
Log.d(LOG_TAG, "No network available!");
}
return false;

这是一种检查它是否有 wifi 连接或数据连接的方法。

  private boolean checkInternetConnection()
{

ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);

// ARE WE CONNECTED TO THE NET

if (conMgr.getActiveNetworkInfo() != null

&& conMgr.getActiveNetworkInfo().isAvailable()

&& conMgr.getActiveNetworkInfo().isConnected())
{

return true;

}
else
{
return false;

}
}

关于android - 如何检查本地 wifi 上的互联网是否可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8535092/

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