在webViewClient中停止加载

bob*_*hox 9 android webview webviewclient

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

这是我的WebClient:

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);    
        }
    });
Run Code Online (Sandbox Code Playgroud)

我想要的是能够确保在离线时不会触发网址并告知相关信息.

app*_*com 5

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

已经提出了许多变通方法。例如

  1. 覆盖 onPageStarted
  2. 通过 CommonsWare添加 JavascriptInterface
  3. 禁用 WebView 触摸事件

此外,注册 BroadcastReceiver 以侦听网络状态更改并在连接丢失时应用解决方法。 网络侦听器 Android