如何在Activity中正确设置CountDownTimer

时间:2016-08-30 18:32:03

标签: java android android-activity

请帮助,我在一个方法中有一个倒计时器,我需要暂停和恢复。 我设法通过使用onPause中的.cancel()来暂停计时器但是我无法正常重启。 我有两个场景: 场景1-暂停但重新启动时,即使初始计时器被取消,它也会重启两个倒计时器。

    public void playAgain (View view) {

        score = 0;
        numberOfQuestions = 0;
        timerTextView.setText("60s");
        pointsTextView.setText("0/0");
        resultTextView.setText("");
        playAgainButton.setVisibility(View.INVISIBLE);
        returnToMenu.setVisibility(View.INVISIBLE);
        resultDisplayTextView.setVisibility(View.INVISIBLE);
        countDownText.setVisibility(View.VISIBLE);
        button0.setVisibility(View.INVISIBLE);
        button1.setVisibility(View.INVISIBLE);
        button2.setVisibility(View.INVISIBLE);
        button3.setVisibility(View.INVISIBLE);
        sumTextView.setVisibility(View.INVISIBLE);

            countDownTimer = new CountDownTimer(millisInFuture, countDownInterval) {
                @Override
                public void onTick(long timeRemain) {

                    millisInFuture = timeRemain;

                    countDownText.setText(String.valueOf(timeRemain / 1000 - 60));

                    if (timeRemain <= 61000) {

                        timerTextView.setText(String.valueOf(timeRemain / 1000) + "s");
                        button0.setEnabled(true);
                        button1.setEnabled(true);
                        button2.setEnabled(true);
                        button3.setEnabled(true);
                        button0.setVisibility(View.VISIBLE);
                        button1.setVisibility(View.VISIBLE);
                        button2.setVisibility(View.VISIBLE);
                        button3.setVisibility(View.VISIBLE);
                        sumTextView.setVisibility(View.VISIBLE);
                        countDownText.setVisibility(View.INVISIBLE);
                    }

                }


                @Override
                public void onFinish() {

                    timerTextView.setText("0s");



                    if (score > 0) {
                        percentage = score * 100 / numberOfQuestions;
                    } else {
                        percentage = 0;
                    }

                    button0.setEnabled(false);
                    button1.setEnabled(false);
                    button2.setEnabled(false);
                    button3.setEnabled(false);
                    playAgainButton.setVisibility(View.VISIBLE);
                    returnToMenu.setVisibility(View.VISIBLE);
                    resultDisplayTextView.setVisibility(View.VISIBLE);
                    resultTextView.setText(" ");
    }
            }.start();

        generateQuestion();

    }


 @Override
    public void onPause() {

        countDownTimer.cancel();

        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
CountDownTimer countDownTimer1 = new CountDownTimer(millisInFuture, countDownInterval) {
            @Override
            public void onTick(long timeRemain) {

                millisInFuture = timeRemain;

                countDownText.setText(String.valueOf(timeRemain / 1000 - 60));

                if (timeRemain <= 61000) {

                    timerTextView.setText(String.valueOf(timeRemain / 1000) + "s");
                    button0.setEnabled(true);
                    button1.setEnabled(true);
                    button2.setEnabled(true);
                    button3.setEnabled(true);
                    button0.setVisibility(View.VISIBLE);
                    button1.setVisibility(View.VISIBLE);
                    button2.setVisibility(View.VISIBLE);
                    button3.setVisibility(View.VISIBLE);
                    sumTextView.setVisibility(View.VISIBLE);
                    countDownText.setVisibility(View.INVISIBLE);
                }

            }


            @Override
            public void onFinish() {

                timerTextView.setText("0s");



                if (score > 0) {
                    percentage = score * 100 / numberOfQuestions;
                } else {
                    percentage = 0;
                }

                button0.setEnabled(false);
                button1.setEnabled(false);
                button2.setEnabled(false);
                button3.setEnabled(false);
                playAgainButton.setVisibility(View.VISIBLE);
                returnToMenu.setVisibility(View.VISIBLE);
                resultDisplayTextView.setVisibility(View.VISIBLE);
                resultTextView.setText(" ");
}
        }.start();
}

第二种情况只会导致系统崩溃:

    public void playAgain (View view) {

        score = 0;
        numberOfQuestions = 0;
        timerTextView.setText("60s");
        pointsTextView.setText("0/0");
        resultTextView.setText("");
        playAgainButton.setVisibility(View.INVISIBLE);
        returnToMenu.setVisibility(View.INVISIBLE);
        resultDisplayTextView.setVisibility(View.INVISIBLE);
        countDownText.setVisibility(View.VISIBLE);
        button0.setVisibility(View.INVISIBLE);
        button1.setVisibility(View.INVISIBLE);
        button2.setVisibility(View.INVISIBLE);
        button3.setVisibility(View.INVISIBLE);
        sumTextView.setVisibility(View.INVISIBLE);

            countDownTimer = new CountDownTimer(millisInFuture, countDownInterval) {
                @Override
                public void onTick(long timeRemain) {

                    millisInFuture = timeRemain;

                    countDownText.setText(String.valueOf(timeRemain / 1000 - 60));

                    if (timeRemain <= 61000) {

                        timerTextView.setText(String.valueOf(timeRemain / 1000) + "s");
                        button0.setEnabled(true);
                        button1.setEnabled(true);
                        button2.setEnabled(true);
                        button3.setEnabled(true);
                        button0.setVisibility(View.VISIBLE);
                        button1.setVisibility(View.VISIBLE);
                        button2.setVisibility(View.VISIBLE);
                        button3.setVisibility(View.VISIBLE);
                        sumTextView.setVisibility(View.VISIBLE);
                        countDownText.setVisibility(View.INVISIBLE);
                    }

                }


                @Override
                public void onFinish() {

                    timerTextView.setText("0s");



                    if (score > 0) {
                        percentage = score * 100 / numberOfQuestions;
                    } else {
                        percentage = 0;
                    }

                    button0.setEnabled(false);
                    button1.setEnabled(false);
                    button2.setEnabled(false);
                    button3.setEnabled(false);
                    playAgainButton.setVisibility(View.VISIBLE);
                    returnToMenu.setVisibility(View.VISIBLE);
                    resultDisplayTextView.setVisibility(View.VISIBLE);
                    resultTextView.setText(" ");
    }
            }.start();

        generateQuestion();

    }


 @Override
    public void onPause() {

        countDownTimer.cancel();

        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
countDownTimer.start();
}

我用Google搜索,似乎无法找到有效的解决方案。 请帮忙,Galaxy商店拒绝上传我的apk,除非此功能有效。

1 个答案:

答案 0 :(得分:3)

问题是,CountDownTimer是一个相对有限的类。它只有两种方法start()cancel(),还有两种回调,onTick()onFinish()。内置了暂停/恢复功能。

我使用TextView设置了一个非常基本的Activity来可视化CountDownTimer的行为,它正如你想要的那样工作 - 在离开活动时暂停,在你返回时恢复。我在这里发布了简单的代码:

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private TextView mTextView;
    private CountDownTimer mCountDownTimer;

    private static final long INTERVAL = 1000L;
    private long timeRemaining = 60000L;

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

        mTextView = (TextView)findViewById(R.id.text_view);
    }

    @Override
    protected void onResume() {
        super.onResume();

        mCountDownTimer = new CountDownTimer(timeRemaining, INTERVAL) {
            @Override
            public void onTick(long l) {
                mTextView.setText(String.format(Locale.getDefault(), "%d sec.", l / 1000L));
                timeRemaining = l;
            }

            @Override
            public void onFinish() {
                mTextView.setText("Done.");
            }
        };
        mCountDownTimer.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mCountDownTimer.cancel();
    }
}

我首先检查您是否可以运行此代码,然后将基本想法集成到您的项目中。您基本上必须保持CountDownTimer对象之外的剩余时间。