Pause CountDown timer Android - android

I'm making a game for android which has a countdown. The countdown works perfectly, but the problem is that when I pause the game (I stop painting the surfaceview), the countdown continues on.
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
timer = String.valueOf(millisUntilFinished / 1000);
invalidate();
}
public void onFinish() {
}
}.start();
How can I pause the countdown?

You can do so by making a variable timer
static int timer = 60000;
Paas this as an argument:
CountdownTimer ct = new CountdownTimer(timer,1000){
Update your timer variable every time the countdown ticks.
Timer = timer-1000;
.
.
}
Now whenever you want to pause your timer
ct.cancel();
And start it whenever you want by calling
ct.start;

Related

Android countdown timer keeps iterating between previous and current value

Hi I'm working on an app with a countdown timer. When the button is clicked the timer starts based on the user inputs into the text field,when I change the value of the text field and a new value for the timer starts, it flashes and shows the previous countdown value and counts both the values down,flashing between the previous and the current countdown value. How do I get the timer to forget the value from before and only use the current timer value.
I tried using the cancel function however that didn't work, I think it's something in my tick function however I'm not sure what it is.
Here is my code:
CountDownTimer mcountDownTimer = new CountDownTimer(getmonTime(), 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
long hr1=TimeUnit.MILLISECONDS.toHours( millisUntilFinished);
long sub1=hr1*60;
long min1=TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
long sub2=min1*60;
timer.setText(""+String.format("%d hr,%d min, %d sec",
TimeUnit.MILLISECONDS.toHours( millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)-sub1,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)-sub2,
-
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
timer.setText("0");
}
};
mcountDownTimer.start();
}
};
You probably don't cancel the previous one.
Save the instance of the CountDownTimer mcountDownTimer (BTW rename it to mCountDownTimer)
And before setting a new one, cancel the old one using:
mCountDownTimer.cancel();
and only then create the new instance.
E.g:
CountDownTimer mCountDownTimer;
#Override
public void onCreate...
...
if (mCountDownTimer != null) mCountDownTimer.cancel();
mCountDownTimer = new CountDownTimer...

Problems with starting and stopping CountDownTimer with buttons

I have an edit text for entering a time in minutes, a button to start a countdown timer using the minutes entered in the edit text which updates a text view, and a button which I want to cancel the countdown timer before it finishes.
The problems I'm having are...the variable the countdown timer needs (the milliseconds) can't get assigned until I click the start timer button because it's coming from an edit text. So I created the countdown timer inside of the onClick for the start timer button. However...I want another button to cancel the timer...and it doesn't have access to the countdown timer. So that didn't solve anything and I'm confused at how to do this. Any advice would help.
Edit: I just realized I could move the stop timer button with the listener to the start timer button onClick also...under the countdown timer. I'm just confused about how the countdown timer could find the milliseconds variable if I have countdown timer outside of the start timer onClick. I guess moving the stop timer under countdown timer works...but still feels like I'm missing something.
Edit2: I tried moving countDownTimer into onCreate and making milliseconds global...but I have to make countDownTimer final in order for the buttons to be able to use it, and countDownTimer just skips to onFinish.
mTimerTextView = (TextView)findViewById(R.id.timer_text_view);
mTimerEditText = (EditText)findViewById(R.id.timer_edit_text);
mStartTimerButton = (Button)findViewById(R.id.btn_start_timer);
mStartTimerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Getting time in minutes from edit text
int timeEntered = Integer.parseInt(mTimerEditText.getText().toString());
long milliseconds = timeEntered * 60000;
CountDownTimer countDownTimer = new CountDownTimer(milliseconds, 1000) {
#Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000*60)) % 60);
int hours = (int) ((millisUntilFinished / (1000*60*60)) % 24);
mTimerTextView.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
#Override
public void onFinish() {
new AlertDialog.Builder(DailyGoalActivity.this)
.setTitle("Time's Up")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
};
countDownTimer.start();
}
});
mStopTimerButton = (Button)findViewById(R.id.btn_stop_timer);
mStopTimerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// countDownTimer.cancel(); <- How could I do this?
}
});
there are two possible reason for your problem first one is that you need to create countdown timer in onCreate and second one is that you are stopping null countdown timer.
solution-1 you can use a boolean variable to check that timer has start or not and then start timer accordingly on button click.Apart from this initialize timer in on create and start in on click follow this code please follow this link
solution-2 to stop the timer use this code
if(countDownTimer != null) {
countDownTimer .cancel();
countDownTimer = null;
}
Isn't the simple solution to declare CountDownTimer variable globally?
Move this code to onCreate
CountDownTimer countDownTimer = new CountDownTimer(milliseconds, 1000) {
#Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000*60)) % 60);
int hours = (int) ((millisUntilFinished / (1000*60*60)) % 24);
mTimerTextView.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
}
Then start timer in Start button's onClick and cancel in Stop button's onClick.
Hope it helps.

CountDownTimer Check every two seconds

I have one CountDownTimer and i want to do an action for every two wasted seconds.
countdowntimer = new CountDownTimer(10000, 1) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
}
}.start();
For example when it starts time remaining = 10 seconds
When time remaining = 8 seconds I want to do an action
When time remaining = 6 seconds I want to do an action
and so on......
Just check if it is divisible by 2.
#Override
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished/1000;
if ((seconds % 2) == 0)
{ // even number--do some action }
}
This is assuming you are calling the onTick() every second like
countdowntimer = new CountDownTimer(10000, 1000) {
This is probably preferable for you
Setting it up to call every 2 seconds should also work and better but leaving the original answer in case someone needs to call onTick() more often
countdowntimer = new CountDownTimer(10000, 2000) {
then you could just do the action with every call to onTick()
CountDownTimer Docs

Android: CountDownTimer not working

I'm trying to create a countdown timer for my app but after doing this, the TextView where it should show the remaining time doesn't show anything so my timer is not working. I don't know if I am missed something.
Here is my timer:
setContentView(R.layout.layout7);
cronometro = (TextView) findViewById(R.id.cronometro);
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
cronometro.setText(Long.toString(millisUntilFinished / 1000));
}
public void onFinish() {
cronometro.setText("done!");
}
}.start();
Edit: As Simon said and changing some things the timer works now. Is there a way of setting the format into MM:SS
cronometro.setText(DateUtils.formatElapsedTime(millisUntilFinished/1000));

How can I display countdown timer on the canvas in android?

I am making a simple android app in which i want to display countdown timer on canvas.
I just want that when the game starts the countdown timer begins with 60 sec and decrements along.....
Pls can someone suggests me how do i use the countdown timer class?
There's plenty of documentation available if you actually look for it:
// 60 second (60000ms) timer with ticks every second (1000ms)
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Now just replace the mTextField.setText() lines to something that would update your Canvas (or update the variable the Canvas uses for your time notification, then invalidate() the Canvas to display the update).
Even though I believe it's self-explanatory, when onFinish() is called, the CountDownTimer has reached 0 and is finished counting down.

Categories

Resources