webview中的进度条在页面加载完成后保持加载不隐藏

时间:2015-04-10 15:43:17

标签: android android-webview android-progressbar

我在webview中显示进度对话框 但它并没有隐藏在页面上 我浏览了许多SO,它说" progress.dismiss()" 但仍然没有解雇 还没有显示加载,我移动到webview中的其他页面

我的代码是

public class MainActivity extends ActionBarActivity {
private WebView webView;
private final String TAG = MainActivity.class.getSimpleName();
ConnectivityManager cm;
android.net.NetworkInfo wifi;
android.net.NetworkInfo datac;
@Override
protected void onCreate(Bundle savedInstanceState) {
    this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    datac = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if ((wifi != null & datac != null)
            && (wifi.isConnected() | datac.isConnected())) {
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);  //to hide notification bar
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView)findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webView.setWebViewClient(new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                boolean returnValue=true;
                if ((wifi != null & datac != null)
                        && (wifi.isConnected() | datac.isConnected())) {
                    if ((Uri.parse(url).getHost().contains("www.cbazaar") || Uri.parse(url).getHost().contains("payseal.icicibank")) || Uri.parse(url).getHost().contains("www.ccavenue")
                            || Uri.parse(url).getHost().contains("www.braintreepaments") || Uri.parse(url).getHost().contains("www.sandbox")) {

                        returnValue=false;
                    }

                if(returnValue==true){
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                view.getContext().startActivity(intent);
                return returnValue;
                }
                }
                return returnValue;
            }
         @Override
            public void onReceivedError(WebView view, int errorCode,
                                      String description, String failingUrl) {
             webView.loadUrl("javascript:history.go(-1)");

          }
            ProgressDialog pd = null;

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

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                pd.dismiss();
            }
        });

        webView.loadUrl("http://www.cbazaar.com");
    }else{
        //no connection
        setContentView(R.layout.activity_no_internet);
        Button netOnBtn=(Button) findViewById(R.id.netOnButton);
        Button refreshHmeBtn=(Button) findViewById(R.id.refreshHmeBtn);
        netOnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
            }
        });
        refreshHmeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent gotoMainActivity = new Intent (MainActivity.this,MainActivity.class);
                startActivity(gotoMainActivity);
                finish();
            }
        });

    }
}
// Open previous opened link from history on webview when back button pressed

@Override
// Detect when the back button is pressed
public void onBackPressed() {
    if(webView.canGoBack()) {
        webView.goBack();
    } else {
        // Let the system handle the back button
        super.onBackPressed();
    }
}

有人可以帮助我,因为如何调试或者为什么加载不会消失而且在移动到其他页面时不显示

提前致谢,因为这是我的第一个网络应用。

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

private void startWebView(String url) {

    MyView.setWebChromeClient(new WebChromeClient() {
        ProgressDialog progressDialog =  new ProgressDialog(MainActivity.this);
        @Override
        public void onProgressChanged(WebView view, int progress) {
            if (progress < 100) {
                // in standard case YourActivity.this

                progressDialog.setMessage("Loading...");
                progressDialog.show();

            }

            if (progress == 100) {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
            }
        }
    });


    // Javascript enabled on webview
    webView.getSettings().setJavaScriptEnabled(true);

    //Load url in webview
    webView.loadUrl(url);

}

这样,您可以在ProgressDialog中使用真正的进度条。