Question

In my Math test application, I want to set 10s for a question using a progressing bar. Here is my code:

public class Question extends Activity {
private boolean enoughTime = true;
    private ProgressBar progressBar;
    int progressStatus = 0;
    private Handler handler = new Handler();
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    createQuestionView();
    new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (progressStatus < 1000){
                    progressStatus += 1;
                    enoughTime = true;
                    handler.post(new Runnable() {

                        @Override
                        public void run() {
                        // TODO Auto-generated method stub
                            progressBar.setProgress(progressStatus);
                        }
                    });
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (progressStatus >= 500){
                    enoughTime = false;
                }
            }
        }).start();
   while (enoughTime){
      DoSomeThing();
   }
   while (!enoughTime){
      DisplaySocre();
   }

It doesn't work. I think my problem is about while loop but don't know how to fix it. Any recommend? Thank you so much.

No correct solution

OTHER TIPS

I think it is better to use a TimerTask.

http://developer.android.com/reference/java/util/TimerTask.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top