ProgressDialog 不会关闭,重定向 url

ProgressDialog won't dismiss, on redirected url

我已经创建了带有进度对话框的简单 webview,我的问题是什么时候 url 得到重定向的进度对话框不会关闭。例如:我打开 url 喜欢:www.myweb.com/account。然后它重定向到:www.myweb.com/login。有解决办法吗?

//progress dialog
    ProgressDialog loading;

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        loading= new ProgressDialog(MainActivity.this, R.style.MyAlertDialogStyle);
        loading.setMessage("Sedang Memuat...");
        loading.setCancelable(false);
        loading.show();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
            if (loading.isShowing()) {
                loading.dismiss();
            }
        super.onPageFinished(view, url);
    }
webView.setWebViewClient(new WebViewClientClass());
 String url = "http://google.com/";
 webView.getSettings().setJavaScriptEnabled(true);
       webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.loadUrl(url);



private class WebViewClientClass extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            prDialog = new ProgressDialog(NewsActivity.this);
            prDialog.setMessage("Please wait ...");
            prDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if(prDialog!=null){
                prDialog.dismiss();
            }
        }
    }
loading.setCancelable(false);

将此设置为 true 为:

loading.setCancelable(true);

也代替这个:

if (loading.isShowing()) {
                loading.dismiss();
            }
        super.onPageFinished(view, url);

使用这个:

super.onPageFinished(view, url);
if (loading!=null) {
            loading.dismiss();
        }

也尝试评论这一行:

super.onPageFinished(view, url);

另一种方法是,如果它没有以某种方式解雇,你可以隐藏它,在这种情况下你必须这样做:

之后:

loading.show();

添加这个:

loading.setVisibility(View.VISIBLE);

之后:

loading.dismiss();

添加这个:

loading.setVisibility(View.INVISIBLE);

我不认为每个重定向都会调用 onPageFinished,这可能是由于使用了 Iframe 或其他原因。我认为你应该像这样覆盖 WebChromeClient 中的 onProgressChanged

myWebView.setWebChromeClient(new WebChromeClient(){

        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);

           if(newProgress <100){
            prDialog.setMessage("Please wait ...");
            prDialog.show();
        }

          else if (newProgress ==100){
               if(prDialog!=null && prDialog.isShowing()){
                  prDialog.dismiss();
        }
       }
    }