Android Timeout webview如果加载时间超过一定时间

时间:2014-03-31 11:05:11

标签: java android webview android-progressbar

如果加载显示错误消息需要很长时间,我想暂停我的webview。我正在使用setWebViewClient,因为我需要使用public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error)

我一直在环顾四周,看到我可以使用方法onProgressChanged(WebView view, int newProgress)。现在我无法在setWebViewClient中使用此方法,也无法弄清楚如何解决此问题。我遇到的另一个问题是,一旦加载页面,进度条永远不会消失,我也无法在方法public void onPageFinished(WebView view, String url)中添加断点。

网络视图设置方法:

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

所以我遇到的问题是,一旦网页加载,进度条就不会被删除,如果加载了一定的时间,我需要超时webview。它看起来像显示进度条,然后就像它应该消失,然后再次开始加载而不会停止。感谢

1 个答案:

答案 0 :(得分:9)

我已更新您的网页视图设置方法,请查看以下更新的代码。 我添加了一个loaderTimerTask类。添加了计时器代码并发表评论以便更好地理解。如果需要,请更新此代码。

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.
    }
  }
}