请帮忙,我有一个需要暂停和恢复的倒数计时器。
我设法通过在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();
}
我已经用谷歌搜索,似乎找不到有效的解决方案。
请帮助,除非此功能有效,否则Galaxy store拒绝上传我的apk。
最佳答案
事实是,CountDownTimer是一个相对有限的类。它只有两种方法start()
和cancel()
,以及两个回调onTick()
和onFinish()
。没有内置的暂停/恢复功能。
我使用TextView设置了一个非常基本的Activity,以可视化CountDownTimer的行为,并且它可以按您想要的方式工作-离开 Activity 时暂停,返回时恢复。我在这里发布了简单的代码:
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对象之外的剩余时间。
关于java - 如何在Activity中正确设置CountDownTimer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39234721/