Java – how to correctly set countdowntimer in activity
Please help, I have a countdown timer that needs to be paused and resumed. I try to pause the timer by using. Cancel() in onpause, but I can't restart it correctly. I have two situations: scenario 1 - it pauses, but when restarting, it seems to restart two countdown timers even if the initial timer is cancelled
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();
}
The second scenario simply crashes the system:
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();
}
I have used Google search and can't seem to find an effective solution. Please help. Unless this function is effective, the galaxy store refuses to upload my APK
resolvent:
The fact is that countdowntimer is a relatively limited class. It has only two methods start() and cancel(), and two callbacks ontick() and onfinish(). There is no built-in pause / resume function
I used textview to set up a very basic activity to visualize the countdowntimer behavior, and it can work as you want - pause when leaving the activity and resume when returning. I released a simple code here:
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();
}
}
I'll start by checking if you can run this code, and then integrate the basic idea into your project. Essentially, you must keep the remaining time outside the countdowntimer object