CountDownTimer继续运行甚至声明cancel()

时间:2017-09-20 15:23:45

标签: java android timer countdowntimer

public class GameActivity extends AppCompatActivity {
    TextView txtTurn, txtScore, txtQuestion, txtQuestionNumber, txtTimer;
    Button[] btnAnswers;
    Player players[];
    ArrayList<Question> questions;
    Question currentQuestion;
    CountDownTimer gameTime;
    Boolean answered, timesup;

    int score, turn, round;
    final int TIMES = 11000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        getSupportActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        txtTurn = (TextView)findViewById(R.id.txtTurnGame);
        txtScore = (TextView)findViewById(R.id.txtScoreGame);
        txtTimer = (TextView)findViewById(R.id.txtTimer);
        txtQuestionNumber = (TextView)findViewById(R.id.questionNumber);
        txtQuestion = (TextView)findViewById(R.id.txtQuestion);
        btnAnswers = new Button[4];
        btnAnswers[0] = (Button)findViewById(R.id.answer1);
        btnAnswers[1] = (Button)findViewById(R.id.answer2);
        btnAnswers[2] = (Button)findViewById(R.id.answer3);
        btnAnswers[3] = (Button)findViewById(R.id.answer4);
        answered = false;
        timesup = false;

        Intent intent = getIntent();
        players = MainActivity.players;

        turn = intent.getExtras().getInt("turn");
        score = players[turn].getScore();
        round = intent.getExtras().getInt("round");
        questions = MainActivity.shuffledQuestionList;
        currentQuestion = questions.get(round/2);

        Integer[] randomAnswer = new Integer[4];
        for (int i = 0; i < randomAnswer.length; i++) {
            randomAnswer[i] = i;
        }
        Collections.shuffle(Arrays.asList(randomAnswer));

        txtTurn.setText(TurnActivity.txtTurn1.getText());
        txtScore.setText("Score : " + Integer.toString(score));
        txtQuestion.setText(currentQuestion.getQuestion());
        txtQuestionNumber.setText("QUESTION #" + Integer.toString(round/2+1));
        for (int i = 0; i < randomAnswer.length; i++){
            String[] answers = currentQuestion.getAnswersList();
            btnAnswers[i].setText(answers[randomAnswer[i]]);
        }

        gameTime = new CountDownTimer(TIMES, 1000){
            public void onTick(long millisUntilFinished) {
                if(!timesup)
                    txtTimer.setText("Time : " + millisUntilFinished / 1000 + "s");
            }

            public void onFinish() {
                txtTimer.setText("Time's Up");
                timesup = true;
                finishTurn();
            }
        };
        gameTime.start();
    }

    public void answerChoose(View v){
        if(!answered && !timesup){
            answered = true;
            gameTime.cancel();
            Button btnPressed = ((Button) v);
            if(btnPressed.getText().equals(currentQuestion.getAnswer())){
                btnPressed.setBackgroundColor(Color.GREEN);
                players[turn].setScore(score+1);
            }
            else{
              btnPressed.setBackgroundColor(Color.RED);
            }

            finishTurn();
        }
    }

    private void finishTurn(){
        gameTime.cancel();
        if(round+1 >= 10){
            final Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
            startActivity(intent);
            finish();
        }
        else {
            final Intent intent = new Intent(getApplicationContext(), TurnActivity.class);
            new CountDownTimer(2000, 1000) {
                public void onTick(long millisUntilFinished) {

                }

                public void onFinish() {
                    //Toast.makeText(getApplicationContext(), "Player2 Time!", Toast.LENGTH_SHORT).show();
                    intent.putExtra("turn", turn);
                    intent.putExtra("round", round + 1);
                    startActivity(intent);
                    finish();
                }
            }.start();
        }
    }

所以,我尝试做一个问答游戏。有4个按钮作为答案选择。当用户单击(任意)按钮时,它会引用answerChoose方法来停止计时器并使用TurnActivity(finishTurn方法)。计时器显示为TextView,当然它已停止,TextView不再更改(似乎计时器已停止)

public void answerChoose(View v){
    if(!answered && !timesup){
        answered = true;
        gameTime.cancel();
        Button btnPressed = ((Button) v);
        //Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
        if(btnPressed.getText().equals(currentQuestion.getAnswer())){
            btnPressed.setBackgroundColor(Color.GREEN);
            players[turn].setScore(score+1);
        }
        else{
            btnPressed.setBackgroundColor(Color.RED);
        }

        finishTurn();
    }
}

但有些事情发生得非常奇怪,在达到TurnActivity后,计时器似乎没有停止,计时器一直运行直到到达终点并再次对TurnActivity产生意图(再次)。如您所见,我在CountDownTimer的onFinish方法中调用了finishTurn方法。因此,即使在answerChoose方法中声明的cancel方法,计时器本身也不会停止。

gameTime = new CountDownTimer(TIMES, 1000){
        public void onTick(long millisUntilFinished) {
            if(!timesup)
                txtTimer.setText("Time : " + millisUntilFinished / 1000 + "s");
        }

        public void onFinish() {
            txtTimer.setText("Time's Up");
            timesup = true;
            finishTurn();
        }
    };

1 个答案:

答案 0 :(得分:0)

问题解决了。我所要做的就是覆盖onStop方法。

@Override
protected void onStop() {
    super.onStop();
    gameTime.cancel();
}