Android中的一组任务的重复ProgressBar活动

时间:2016-07-04 17:40:02

标签: android multithreading android-progressbar

我是android的新手,没有多少经验来管理线程。我正在进行一项活动,我想显示5秒的进度条,然后重复。在这5秒钟内,我将显示一些文本供用户处理文本。我想重复这个说N次。

目前,我有以下代码适用于此类进展。我尝试循环它但它没有帮助,因为线程同时执行。我怎么能重复N次?我是在正确的道路上解决我的问题吗?

public class test extends Activity {

    private ProgressBar progressBar;
    private int progressStatus = 0;
    private TextView textView;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loop);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        textView = (TextView) findViewById(R.id.textView1);
        progressBar.setScaleY(3f);
        // Start long running operation in a background thread

        for(int i=0; i<5; i++)
            Progress();

    }

    public void Progress(){
        new Thread(new Runnable() {
            public void run() {
                while (progressStatus < 100) {
                    progressStatus += 1;
                    // Update the progress bar and display the
                    //current value in the text view
                    handler.post(new Runnable() {
                        public void run() {
                            progressBar.setProgress(progressStatus);
                            textView.setText(progressStatus+"/"+progressBar.getMax());
                        }
                    });
                    try {
                        // Sleep for 200 milliseconds.
                        //Just to display the progress slowly
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否会建议您使用您所遵循的模式,但这是让线程一个接一个地运行而不是同时运行的方法:

public void progress(final int numberOfRuns) {
    if (numberOfRuns <= 0 ) {
        return;
    }
    new Thread(new Runnable() {
        public void run() {
            while (progressStatus < 100) {
                progressStatus += 1;
                // Update the progress bar and display the
                //current value in the text view
                handler.post(new Runnable() {
                    public void run() {
                        progressBar.setProgress(progressStatus);
                        textView.setText(progressStatus + "/" + progressBar.getMax());
                        // For the UI Changes. Eg update the loop number
                        myTextView.setText(Integer.toString(totalLoop));
                    }
                });
                try {
                    // Sleep for 200 milliseconds.
                    //Just to display the progress slowly
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            progressStatus = 0;
            totalLoop = totalLoop+1;
            progress(numberOfRuns - 1);
        }
    }).start();
}

然后只需调用progress(numberOfRuns),不需要任何循环。