Android:进度栏加载网址,继续加载

时间:2016-04-01 17:04:11

标签: android

在我的应用程序中,我使用webview打开网址。网址非常简单,加载只需几秒钟。但是当网址完全加载时,进度条不会隐藏。我无法弄清楚什么是错的。请帮我。下面我发布了代码。

WebView类

public class noti_cse extends BaseActivity {
    private WebView webView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.noti_cse);
        //  overridePendingTransition(0, 0);

        //  webView = (WebView) findViewById(R.id.webcse);
        //  webView.setWebViewClient(new MyWebViewClient());
        if (!DetectConnection.checkInternetConnection(this)) {
            Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
        } else {
            webView = (WebView) findViewById(R.id.webcse);
            webView.setWebViewClient(new CustomWebViewClient());
            webView.clearCache(true);
            webView.clearHistory();
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            webView.getSettings().setBuiltInZoomControls(true);
            startWebView("http://notify.php");
        }


        // webView.getSettings().setJavaScriptEnabled(true);
        //  startWebView("http://notify.php");
        // webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);

        // webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);


    }


    private void startWebView(String url) {

        //Create new webview Client to show progress dialog
        //When opening a url or click on link

        webView.setWebViewClient(new WebViewClient() {
            ProgressDialog progressDialog;

            //If you will not use this method url links are opeen in new brower not in webview
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            //Show loader on url load
            public void onLoadResource(WebView view, String url) {
                if (progressDialog == null) {
                    // in standard case YourActivity.this
                    progressDialog = new ProgressDialog(noti_cse.this);
                    progressDialog.setMessage("On the Way!!!");
                    progressDialog.setCanceledOnTouchOutside(false);
                    progressDialog.show();
                }
            }

            public void onPageFinished(WebView view, String url) {
                try {
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                        progressDialog = null;
                    }
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }

        });
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(url);

    }

    private class CustomWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (!DetectConnection.checkInternetConnection(noti_cse.this)) {
                Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
            } else {
                view.loadUrl(url);
            }
            return true;
        }
    }

    @Override
    protected boolean useDrawerToggle() {
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_placements)
            return true;

        if (item.getItemId() == android.R.id.home)
            onBackPressed();

        return super.onOptionsItemSelected(item);
    }

1 个答案:

答案 0 :(得分:0)

您正在设置2 WebViewClient。

首先:

webView.setWebViewClient(new CustomWebViewClient()); 

之后,你正在打电话

startWebView()

设置新的WebviewClient。

您可以更改以下方法:

private void startWebView(String url) {

    //Create new webview Client to show progress dialog
    //When opening a url or click on link

    webView.setWebViewClient(new WebViewClient() {
        ProgressDialog progressDialog;

        //If you will not use this method url links are opeen in new brower not in webview
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (!DetectConnection.checkInternetConnection(noti_cse.this)) {
            Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
            } else {
                view.loadUrl(url);
            }
            return true;
        }

        //Show loader on url load
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon){
            if (progressDialog == null) {
                // in standard case YourActivity.this
                progressDialog = new ProgressDialog(noti_cse.this);
                progressDialog.setMessage("On the Way!!!");
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.show();
            }
            super.onPageStarted(view, url, favicon);
        }

        public void onPageFinished(WebView view, String url) {
            try {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

    });
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(url);

}

并删除此行格式onCreate()方法:

webView.setWebViewClient(new CustomWebViewClient());