gpt4 book ai didi

android - 在 WebView 中检查网站是否能够加载

转载 作者:行者123 更新时间:2023-11-30 03:06:44 26 4
gpt4 key购买 nike

我在 WebView 中加载网站。有时,当用户在设备中没有互联网连接时,如果用户尝试加载 WebView,则会显示加载网站时出错。通过它,用户开始了解网站网址。

如何防止这个问题?

最佳答案

检查这个:

 public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {

NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {

return true;
}

}

return false;
}

现在调用它:

   if(isConnectingToInternet()) {

// Load your website in webview

} else {
// No Internet connection

}

关于android - 在 WebView 中检查网站是否能够加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21694121/

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