gpt4 book ai didi

android - 在 webViewClient 中停止加载

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

在我的 WebView 中,标题中有多个链接。在离线模式下,我不允许重新加载页面并保持该页面不变,然后通知用户有关互联网连接的信息。如果我单击其他链接,我可以使用 shouldOverrideUrlLoading 在加载之前捕获链接。但是,如果我单击相同的链接,则不会触发 shouldOverrideUrlLoading,而是首先调用 onLoadResource 方法。在该方法中,我尝试了 webView.stopLoading(); 但它继续工作并触发 onReceivedError 方法并在页面上显示错误报告。

这是我的网络客户端:

webView.setWebViewClient(new WebViewClient() {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(MyApplication.getInstance().isOnline()) {
return false;
}

Toast.makeText(getApplicationContext(), "Please check Internet connection and try again.", Toast.LENGTH_SHORT).show();
return true;
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("ON_PAGE_START", "RUNNING ============>" + url);
if (MyApplication.getInstance().isOnline()) {
super.onPageStarted(view, url, favicon);
} else {
webView.stopLoading();
}
}

@Override
public void onLoadResource(WebView view, String url) {

if (MyApplication.getInstance().isOnline()) {
super.onLoadResource(view, url);
} else {
webView.stopLoading();
}
}

@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

view.stopLoading();

Toast.makeText(getApplicationContext(), "Please check Internet connection and try again.", Toast.LENGTH_SHORT).show();
super.onReceivedError(view, errorCode, description, failingUrl);
}
});

我想要的是能够确保 url 在脱机时不会被触发并通知相关信息。

最佳答案

这是一个已知问题,并报告了一个错误。 http://code.google.com/p/android/issues/detail?id=2887

已经提出了许多解决方法。例如

  1. Override onPageStarted
  2. Add JavascriptInterface通过 CommonsWare
  3. Disable WebView touch events

此外,注册一个 BroadcastReceiver 来监听网络状态变化,并在连接丢失时应用解决方法。 Network listener Android

关于android - 在 webViewClient 中停止加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15492795/

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