Android:在应用程序启动时启动进度条

时间:2014-04-03 14:51:30

标签: android

当我点击按钮时,我会显示代码,显示进度条对话框。

public class MyAndroidAppActivity extends Activity {

Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();

private long fileSize = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

}

public void addListenerOnButton() {

    btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
    btnStartProgress.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // prepare for a progress bar dialog
            progressBar = new ProgressDialog(v.getContext());
            progressBar.setCancelable(true);
            progressBar.setMessage("File downloading ...");
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setProgress(0);
            progressBar.setMax(100);
            progressBar.show();

            //reset progress bar status
            progressBarStatus = 0;



            new Thread(new Runnable() {
                public void run() {
                    while (progressBarStatus < 100) {

                        // process some tasks
                        progressBarStatus = doSomeTasks();

                        // your computer is too fast, sleep 1 second
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        // Update the progress bar
                        progressBarHandler.post(new Runnable() {
                            public void run() {
                                progressBar.setProgress(progressBarStatus);
                            }
                        });
                    }


                    if (progressBarStatus >= 100) {

                        // sleep 2 seconds, so that you can see the 100%
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        // close the progress bar dialog
                        progressBar.dismiss();
                    }
                }
            }).start();

        }

    });

}



// file download simulator... a really simple
public int doSomeTasks() {

    Intent intent = new Intent(MyAndroidAppActivity.this, Accueil.class);
    startActivity(intent);

}

}

但是我想在Android应用程序的开头显示它(进度条对话框)。 我怎样才能做到这一点 ?提前感谢您的帮助。

0 个答案:

没有答案