How to set up a timer for playing Android?

I want to program a game for Android.

Principle of the game: the user must have a short time to select the correct answer.

My problem is the combination between input (choice of answer) and time (countdown). I tried to start the stream, but the stream does not stop. Thus, the user gives an answer, the next action will be shown and after a while (when the "timer" becomes 0), the activity will be shown again.

  • How could I implement this correctly?
  • Should i use Thread, Handler, CountDownTimer?
+3
source share
3 answers

You can save the timer start using this during initialization:

Timer updateTimer = new Timer("update");
updateTimer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
    updateGUI();
        }
    }, 0, 1000);
long startTime = System.currentTimeMillis();

Then in the stream:

//Clock update
currentTime = System.currentTimeMillis() - startTime;
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
clock.setText(sdf.format(currentTime));
clock.invalidate();

, ?

+2

, , , , . , :/

boolean userHasAnswered = false.

(, ) . , userHasAnswered = true.

, , .

ans, , loadNextQuestion().

, , userHasAnswered == true, , updateScore() loadNextQuestion().

0

You can see the alarm manager

0
source

All Articles