如何在页面加载超过30秒后停止网页视图并显示重新加载消息?

huangapple go评论72阅读模式
英文:

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

问题

以下是你的代码的翻译部分:

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);
        }
    };
}

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);
    }
}

顶部有你在主活动中的代码。

英文:

I have a web view and i have this code (P.S i am a beginer in programming)

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);
}
};
}

I already have webview client and webchrome client, you can see and download my code files here (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);
}
}

At the top you can see my code in Main activity.

答案1

得分: 0

你可以在这个链接中查看如何判断 WebView 是否完全加载完成:
这里

一旦你开始加载 WebView,你可以启动一个处理程序,然后检查页面是否完全加载完成,或者你可以重新加载网页。

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // 在30秒后检查 WebView 是否完全加载完成
        
        // 如果页面没有加载完成,则调用以下行重新加载
        // mWebview.loadUrl("http://www.google.com");
    }
}, 30000);
英文:

You can check how to find if the webview loaded completely at this link :
here

Once you start loading the webview, you can start a handler and check if the page is loaded completely, or you can reload the webpage.

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);

答案2

得分: 0

覆盖WebviewClient类,在onPageStarted方法中启动一个倒计时计时器,在onPageFinished方法中取消计时器,在倒计时计时器的onFinish中调用您的方法。

英文:

Override Webviewclient class and start a countdowntimer in onPageStarted method and cancel the timer in onPageFinished , invoke your method in onfinish of countdowntimer.

huangapple
  • 本文由 发表于 2020年10月7日 22:22:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64246118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定