如果需要的Andr​​oid超时网页视图超过一定时间更长的时间来装载 [英] Android Timeout webview if it takes longer than a certain time to load

查看:210
本文介绍了如果需要的Andr​​oid超时网页视图超过一定时间更长的时间来装载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我超时的WebView,如果它需要很长的时间来加载显示错误信息。我使用 setWebViewClient ,因为我需要使用公共无效onReceivedSslError(的WebView视图,SslErrorHandler处理器,SslError错误)

I Want to timeout my webview if it takes a long time to load showing an error message. I'm using setWebViewClient because I need to use the public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error).

我一直在四处寻找,看见我可以使用的方法 onProgressChanged(的WebView观点,诠释newProgress)。现在我不能使用 setWebViewClient 这种方法并不能弄清楚如何去解决这个问题。另一个问题我是进度条永远不会消失,一旦加载页面时,我不能断点添加到方法公共无效onPageFinished(的WebView视图,字符串URL)或者

I've been looking around and saw I can use the method onProgressChanged(WebView view, int newProgress). Now I can't use this method in setWebViewClient and can't figure out how to go about solving this problem. Another issue I have is that the progress bar never goes away once the page is loaded, I can't add a breakpoint to the method public void onPageFinished(WebView view, String url) either.

Web视图设置方法:

public void WebViewSettings(){

    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.canGoBack();

    webView.setWebViewClient(new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals(urlString)) {
                // This is my web site, so do not override; let my WebView load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            // Check to see if there is a progress dialog
            if (progressDialog == null) {
                progressDialog = new ProgressDialog(context);
                progressDialog.setTitle("Loading...");
                progressDialog.setMessage("Please wait.");
                //progressDialog.setCancelable(false);
                progressDialog.setIndeterminate(true);
                progressDialog.show();
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // Page is done loading;
            // hide the progress dialog and show the webview
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;
                webView.setEnabled(true);
            }
        }

        @Override
        public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
             handler.proceed();
        }

        @Override
        public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
            Toast.makeText(context, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
        }
    });
}

所以我的问题是,一旦网页加载进度条不会被删除,我需要超时web视图,如果它接管了一定的时间来加载。它看起来像显示进度条,然后消失了像它应该,然后重新开始,并不会停止加载。谢谢

So the issues I have are the progress bar doesn't get removed once the web page has loaded and I need to timeout the webview if it take over a certain amount of time to load. It looks like the progress bar is shown, then is disappears like it should, then starts loading again and wont stop. Thanks

推荐答案

我已经更新您的WebView设置方法请在下面找到更新code。
我添加了一个loaderTimerTask类。增加了code定时器和更多的理解提出意见。如果您有需要请更新此code。

I have updated your webview setting method please find below updated code. I have added one loaderTimerTask class. Added code for timer and put comment for more understanding. Please update this code if you need.

private boolean isPageLoadedComplete = false; //declare at class level

public void WebViewSettings(){

webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.canGoBack();

 /**
  *I had put code here
  */
Timer myTimer = new Timer();
    //Start this timer when you create you task
myTimer.schedule(loaderTask, 3000); // 3000 is delay in millies

webView.setWebViewClient(new WebViewClient(){

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals(urlString)) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        // Check to see if there is a progress dialog
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(context);
            progressDialog.setTitle("Loading...");
            progressDialog.setMessage("Please wait.");
            //progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        }
    }

    @Override
    public void onPageFinished(WebView view, String url) {
            isPageLoadedComplete = true;
        // Page is done loading;
        // hide the progress dialog and show the webview
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
            webView.setEnabled(true);
        }
    }

    @Override
    public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
         handler.proceed();
    }

    @Override
    public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
        Toast.makeText(context, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
    }
});
}

 /**
  *This class is invoke when you times up.
  */
 class loaderTask extends TimerTask {
  public void run() {
    System.out.println("Times Up");
    if(isPageLoadedComplete){
    }else{
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
            webView.setEnabled(true);
        }
        //show error message as per you need.
    }
  }
}

这篇关于如果需要的Andr​​oid超时网页视图超过一定时间更长的时间来装载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆