ProgressDialog未显示进度

时间:2016-08-14 04:44:57

标签: android progressdialog

我使用了ProgressDialog用法的简单示例。这段代码的作者确信他的代码是正确的并且运作良好。

gem install sqlite3

但是当我在我的项目中运行此代码时,我发现ProgressDialog始终显示0值作为进度。我错了什么?

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

private ProgressDialog barProgressDialog;

public void launchBarDialog() {
        barProgressDialog = new ProgressDialog(this);

        barProgressDialog.setTitle("Downloading Image ...");
        barProgressDialog.setMessage("Download in progress ...");
        barProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        barProgressDialog.setProgress(0);
        barProgressDialog.setMax(20);
        barProgressDialog.show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // Here you should write your time consuming task...
                    while (barProgressDialog.getProgress() <= barProgressDialog.getMax()) {
                        Thread.sleep(2000);
                        barProgressDialog.incrementProgressBy(2);

                        if (barProgressDialog.getProgress() == barProgressDialog.getMax()) {
                            barProgressDialog.dismiss();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        Log.d("", BuildConfig.VERSION_NAME);
    }