java - 如果加载时间超过特定时间,Android Timeout webview

标签 java android webview android-progressbar

如果加载显示错误消息需要很长时间,我想使我的 webview 超时。我正在使用 setWebViewClient,因为我需要使用 public void onReceivedSslError(WebView View 、SslErrorHandler 处理程序、SslError 错误)

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

网页 View 设置方法:

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 超时。看起来进度条显示了,然后它应该消失了,然后再次开始加载并且不会停止。谢谢

最佳答案

我已经更新了您的 webview 设置方法,请在下面找到更新的代码。 我添加了一个 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.
    }
  }
}

关于java - 如果加载时间超过特定时间,Android Timeout webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22759978/

相关文章:

java - 如何绘制透明线?

java - 枚举类声明并在另一个类中使用它

android - 使用 SimpleCursorAdapter 和 SQLite 在 ListView 中反转顺序

android - Tensorflow 图像分类 Android 示例无法构建

android - 移动设备(平板电脑)的可视化工具

android - 作为网站启动的移动应用程序

android - Android 中 Youtube 网站上的 WebView 问题?

java - 在 JSF 中实现项目编辑页面

JavaFX:AnimationTimer与MenuBar的交互

java - 网页 View 应用程序无法运行