如果页面未加载 30 秒,如何停止 Web 视图并显示重新加载消息?

how to stop the web view and display a reload message if the page does not load for 30 seconds?

我有一个网络视图,我有这段代码(P.S我是编程初学者)

private WebChromeClient getChromeClient() {


        return new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                progressDialog.show();
                if (newProgress ==100){
                    progressDialog.dismiss();
                }
                super.onProgressChanged(view, newProgress);
                mWebView.setVisibility(View.GONE);

            }
        };
    }

我已经有webview客户端和webchrome客户端,你可以在这里查看和下载我的代码文件(cloud.mail.ru/public/2hHz/25DMkxh3U)

class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            mWebView.setVisibility(View.GONE);
            super.onPageStarted(view, url, favicon);

        }

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

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (mbErrorOccured == false && mbReloadPressed) {
                hideErrorLayout();
                mbReloadPressed = false;
            }
            super.onPageFinished(view, url);
            mWebView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            mbErrorOccured = true;
            showErrorLayout();
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    }

在顶部你可以在 Main activity 中看到我的代码。

您可以在此处 link 检查如何查找 webview 是否已完全加载: here

开始加载 webview 后,您可以启动处理程序并检查页面是否已完全加载,或者您可以重新加载网页。

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // check if the webview is loaded completely after 30 seconds here
            
            //if the page is not loaded then call the below line to reload
            // mWebview.loadUrl("http://www.google.com");
        }
    }, 30000);

重写 Webviewclient class 并在 onPageStarted 方法中启动倒数计时器并在 onPageFinished 中取消计时器,在倒数计时器的 onfinish 中调用您的方法。