调用cancel()方法时,CountDownTimer不会停止

时间:2017-09-08 14:38:55

标签: java android

我做了一个名为" The Amazing Race"的简单游戏。 该计划概述:一个有三只动物参与比赛的计划,包括猪,鸡和乌龟,将让玩家猜出哪种动物将在比赛中获胜。 但是,我在CountDownTimer对象的cancel()方法中苦苦挣扎。我无法理解为什么cancel()方法不起作用(即使其中一只动物到达目的地,但其他两只仍然跑到最后)

                    countDownTimer = new CountDownTimer(10000, 300) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        Random rd = new Random();

                        if (skbGa.getProgress() >= skbGa.getMax()) {
                            this.cancel();
                            Toast.makeText(MainActivity.this, "Gà thắng", Toast.LENGTH_SHORT).show();
                        }
                        if (skbLon.getProgress() >= skbLon.getMax()) {
                            this.cancel();
                            Toast.makeText(MainActivity.this, "Lợn thắng", Toast.LENGTH_SHORT).show();
                        }
                        if (skbRua.getProgress() >= skbRua.getMax()) {
                            this.cancel();
                            Toast.makeText(MainActivity.this, "Rùa thắng", Toast.LENGTH_SHORT).show();
                        }

                        skbGa.setProgress(skbGa.getProgress() + (rd.nextInt(10) + 1));
                        skbLon.setProgress(skbLon.getProgress() + (rd.nextInt(10) + 1));
                        skbRua.setProgress(skbRua.getProgress() + (rd.nextInt(10) + 1));
                    }

                    @Override
                    public void onFinish() {

                    }
                };

                countDownTimer.start();

我的程序的整个代码

package quynhanhhle.theamazingrace;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {
    TextView txtDiem;
    CheckBox chkGa, chkRua, chkLon;
    SeekBar skbGa, skbRua, skbLon;
    ImageButton btnBatDau;
    CountDownTimer countDownTimer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        anhXa();

        xuLyCheckBox();

        chuongTrinhChinh();

        choiLai();
    }

    private void choiLai() {
        btnBatDau.setVisibility(View.VISIBLE);
        chkRua.setEnabled(true);
        chkLon.setEnabled(true);
        chkGa.setEnabled(true);
    }

    private void chuongTrinhChinh() {
        btnBatDau.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!(chkLon.isChecked() || chkGa.isChecked() || chkRua.isChecked())) {
                    Toast.makeText(MainActivity.this, "Đoán con vật chiến thắng", Toast.LENGTH_SHORT).show();
                } else {
                    btnBatDau.setVisibility(View.INVISIBLE);
                    chkGa.setEnabled(false);
                    chkLon.setEnabled(false);
                    chkRua.setEnabled(false);

                    countDownTimer = new CountDownTimer(10000, 300) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            Random rd = new Random();

                            if (skbGa.getProgress() >= skbGa.getMax()) {
                                this.cancel();
                                Toast.makeText(MainActivity.this, "Gà thắng", Toast.LENGTH_SHORT).show();
                            }
                            if (skbLon.getProgress() >= skbLon.getMax()) {
                                this.cancel();
                                Toast.makeText(MainActivity.this, "Lợn thắng", Toast.LENGTH_SHORT).show();
                            }
                            if (skbRua.getProgress() >= skbRua.getMax()) {
                                this.cancel();
                                Toast.makeText(MainActivity.this, "Rùa thắng", Toast.LENGTH_SHORT).show();
                            }

                            skbGa.setProgress(skbGa.getProgress() + (rd.nextInt(10) + 1));
                            skbLon.setProgress(skbLon.getProgress() + (rd.nextInt(10) + 1));
                            skbRua.setProgress(skbRua.getProgress() + (rd.nextInt(10) + 1));
                        }

                        @Override
                        public void onFinish() {

                        }
                    };

                    countDownTimer.start();
                }
            }
        });

    }

    private void xuLyCheckBox() {
        chkGa.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (chkGa.isChecked()){
                    chkLon.setChecked(false);
                    chkRua.setChecked(false);
                }
            }
        });

        chkLon.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (chkLon.isChecked()){
                    chkGa.setChecked(false);
                    chkRua.setChecked(false);
                }
            }
        });

        chkRua.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (chkRua.isChecked()){
                    chkGa.setChecked(false);
                    chkLon.setChecked(false);
                }
            }
        });
    }

    private void anhXa() {

        txtDiem = (TextView) findViewById(R.id.txtDiem);

        chkGa = (CheckBox) findViewById(R.id.chkGa);

        chkLon = (CheckBox) findViewById(R.id.chkLon);

        chkRua = (CheckBox) findViewById(R.id.chkRua);

        skbGa = (SeekBar) findViewById(R.id.skbGa);

        skbLon = (SeekBar) findViewById(R.id.skbLon);

        skbRua = (SeekBar) findViewById(R.id.skbRua);

        btnBatDau = (ImageButton) findViewById(R.id.btnBatDau);
    }
}

2 个答案:

答案 0 :(得分:0)

问题是你每次点击btnBatDau时都会创建一个新的countDownTimer。

我的建议是将countDownTimer初始化块放到Activity的onCreate()上。

在您的示例中调用CountDownTimer#cancel()(或this.cancel())没有问题。)它取消了计时器,并且不会调用onTick()。

请参阅此示例。它取消正确:

 CountDownTimer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        timer = new CountDownTimer(10000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if(millisUntilFinished < 5000) {
                    this.cancel();
                }

                Log.d("Timer", millisUntilFinished + "");
            }

            @Override
            public void onFinish() {

            }
        };

        timer.start();
    }

日志输出如下:

09-08 17:48:10.627 21140-21140/com.safaorhan.coundownsample D/Timer: 8954
09-08 17:48:11.629 21140-21140/com.safaorhan.coundownsample D/Timer: 7953
09-08 17:48:12.630 21140-21140/com.safaorhan.coundownsample D/Timer: 6952
09-08 17:48:13.631 21140-21140/com.safaorhan.coundownsample D/Timer: 5950
09-08 17:48:14.633 21140-21140/com.safaorhan.coundownsample D/Timer: 4949

答案 1 :(得分:0)

您可以像这样实现自己的计时器

Map<Class<? extends ViewModel>, Provider<ViewModel>>