启动Android应用程序时“加载”消息不会消失

时间:2014-11-22 18:01:54

标签: android webview loading progressdialog

我想展示一个" loading ..."启动我的Android应用时的消息。它应该出现3秒钟。

我尝试使用下面的代码实现这一点,但问题是它一直显示"正在加载......"信息。当我点击背景时,它会消失,但在使用此代码时,它绝对不是我想到的。

有人可以帮我解决这个问题吗?

public class MainActivity extends Activity {

//private Button button;
private WebView webView;
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

    //Get webview 
    webView = (WebView) findViewById(R.id.webView1);

    startWebView("http://example.com");

}

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() {      


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

    });

    webView.setWebChromeClient(new WebChromeClient() {
        //Show loader on url load
        ProgressDialog progressDialog;
        public void onProgressChanged(WebView view, int progress) {
            if (progressDialog == null) {
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }

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

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

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

}

// 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 :(得分:0)

也许你可以这样做:

progressDialog.show();
new Handler().postDelayed(new Runnable() {
  @Override
  public void run() {
   progressDialog.dismiss();
  }
}, 3000);

这将在3秒后自动关闭进度对话框