gpt4 book ai didi

android - 如何查找互联网是否正常工作

转载 作者:行者123 更新时间:2023-11-30 00:56:16 24 4
gpt4 key购买 nike

在设备中,当我启用互联网数据时,它在网络中显示 E。但我的 SIM 卡中没有数据包,互联网在任何应用程序中都无法使用。我已经使用了这段代码,它返回 true 互联网在那里。在这种情况下,如何确定互联网是否正常工作:

 public static boolean isConnectingToInternetLatest(Context mContext) {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Network[] networks = connectivityManager.getAllNetworks();
NetworkInfo networkInfo;
for (Network mNetwork : networks) {
networkInfo = connectivityManager.getNetworkInfo(mNetwork);
if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
return true;
}
}
} else {
if (connectivityManager != null) {
//noinspection deprecation
NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
if (info != null) {
for (NetworkInfo anInfo : info) {
if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
//Log.d("Network", "NETWORK NAME: " + anInfo.getTypeName());
return true;
}
}
}
}
}
return false;
}

最佳答案

public boolean isInternetWorking() {
boolean success = false;
try {
URL url = new URL("https://google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(10000);
connection.connect();
success = connection.getResponseCode() == 200;
} catch (IOException e) {
e.printStackTrace();
}
return success;
}

关于android - 如何查找互联网是否正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40111439/

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