Save Countdowntimer on activity destroy and resume it in onResume() - android

I want to start a counter for 2 minute and when activity destroy then i want to save that and on activity resume i want to start the counter where i left
counter = new CountDownTimer(120000, 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished)
{
textview.setText("" + String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
Log.i(TAG, "onTick: "+millisUntilFinished);
}
public void onFinish()
{
textview.setText("done!");
}
}.start();

We just save a value of millisUntilFinished when activity is destroy and create new CountDownTimer with that value when activity start again.
long millisUntilFinished;
in onDestroy() (or onStop())
#Override
public void onDestroy() {
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putLong("millisUntilFinished", millisUntilFinished);
editor.apply();
super.onDestroy();
}
and get value of millisUntilFinished when you want to start the CountDownTimer
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
millisUntilFinished = prefs.getLong("millisUntilFinished", 120000);
counter = new CountDownTimer(millisUntilFinished, 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
this.millisUntilFinished = millisUntilFinished;
textview.setText("" + String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)
- TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
Log.i(TAG, "onTick: " + millisUntilFinished);
}
public void onFinish() {
textview.setText("done!");
}
}.start();

Store your counter data in shared prefrences in onStop method as onDestroy() is not always guaranteed and start your counter using your stored data from onResume method

Related

Saving countTimer() on interrupts (Android)

I am creating a game and I put a timer to onCreate method to display time to user and make an action when it reaches to 0. My way works correctly but since I wrote it to onCreate the time continues to count down and if I rotate screen etc it resets the timer seconds. I know why this happens but I could not find a way to stop it when app is paused and make it continue from where it left of when application continues. Here is my code
new CountDownTimer(15000, 1000) {
public void onTick(long millisUntilFinished) {
question.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
question.setText("done!");
Intent i2 = new Intent(getApplicationContext(), CategoryActivity.class);
startActivity(i2);
}
}.start();
Should I save milisUntilFinished to savedInstanceState and make the timer start depending on the savedInstanceState or is there an easier solution
Edit: So I updated my code to reflect the thing I ve asked. This way I can save the last state of timer when the phone is rotated, but timer still does not stop when I open the menu (pause the application.)
if(savedInstanceState!=null){
seconds = savedInstanceState.getInt("seconds");
new CountDownTimer(seconds * 1000, 1000) {
public void onTick(long millisUntilFinished) {
seconds = (int) millisUntilFinished;
question.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
question.setText("done!");
Intent i2 = new Intent(getApplicationContext(), CategoryActivity.class);
startActivity(i2);
}
}.start();
}else {
new CountDownTimer(15000, 1000) {
public void onTick(long millisUntilFinished) {
seconds = (int) millisUntilFinished;
question.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
question.setText("done!");
Intent i2 = new Intent(getApplicationContext(), CategoryActivity.class);
startActivity(i2);
}
}.start();
}
CounDwonTimer mCountTimer;
int seconds = 0;
then just cancel the mCountTimer in onPause and start in `onResme'
protected void onResume(){
super.onReusme();
mCountTimer = new CountDownTimer(seconds*1000,1000){
//......record the seconds
}
}
protected void onPause(){
if(mCountTimer != null)
mCountTimer.cancel();
}
if your activity will destory when rotate the screen,you need onSaveInstanceState.otherwise you do not need this method
I hope the following code will help you,
private long startTime = 0L;
private boolean threadStop = false;
private Handler myHandler = new Handler();
private Runnable updateTimerMethod = new Runnable() {
public void run() {
if (isAdded()) {
timeInMillies = SystemClock.uptimeMillis() - startTime;
//finalTime = timeSwap + timeInMillies;
time.setText(miliSecondToMinSec(timeInMillies));
if (!threadStop)
myHandler.postDelayed(this, 1000);
}
}
};
#Override
public void onStart() {
super.onStart();
if (threadStop) {
threadStop = false;
startTime = SystemClock.uptimeMillis() - timeInMillies;
myHandler.postDelayed(updateTimerMethod, 0);
}
}
#Override
public void onStop() {
threadStop = true;
super.onStop();
}

Android, CountDownTimer latency

I'm using CountDownTimer like so:
timer = new CountDownTimer(30 * 1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
handleOnTick(millisUntilFinished);
}
#Override
public void onFinish() {
handleOnFinish();
playSound(ALERT_SECOND_FINISH);
}
};
timer.start();
}
private void handleOnTick(long millisUntilFinished) {
millisUntilFinished = millisUntilFinished / 1000;
tvTime.setText(
String.format(Locale.US,
"%d:%02d",
min, sec)
);
}
The problem is when handleOnTick is called, millisUntilFinished is ~29953 and millisUntilFinished = millisUntilFinished / 1000 equals 29 while I'm expecting 30!
So how can I handle this latency without using counter value or so? I used Bigdecimal and Rounding but it still passes values less than 30000.
Try this: millisUntilFinished = Math.round(millisUntilFinished / 1000.0f);

Continue the count down when i close the app

how can i do a count down that continue when i close the app, even when i turn off the phone.
I tried to search in the internet but it doesn't work.
Someone can help me?
Thanks
This is the code of the count down that i want to contunue
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
tv1.setText("La cuenta llega a 0 en: " + millisUntilFinished / 1000);
}
public void onFinish() {
tv1.setText("Listo!");
}
}.start();

Stop and restart CountDownTimer

I have a Timer which it starts from 00:00 nice, but when I want to stop it I call cdTimer.cancel() but then I'm not able to restart the Timer with the old values and it starts again. I have this method :
private void StartTimer(){
final long EndTime = 3600;
cdtTimer = new CountDownTimer(EndTime*1000, 1000) {
public void onTick(long millisUntilFinished) {
long secondUntilFinished = (long) (millisUntilFinished/1000);
long secondsPassed = (EndTime - secondUntilFinished);
long minutesPassed = (long) (secondsPassed/60);
secondsPassed = secondsPassed%60;
tvCounterTimer.setText(String.format("%02d", minutesPassed) + ":" + String.format("%02d", secondsPassed));
}
public void onFinish() {
tvCounterTimer.setText("done!");
}
}.start();
}
I thought I could save the values minutesPassed and secondsPassed but then I should pass them by parameter, right?
To cancle a timer do this
timer.cancel();
timer.purge();
To restart a timer you will have to re instantiate the object and then schedule it. i will start again.

Countdown timer not looping

Basically I am doing a cardio feature and have three countdown timers in a row nested within each other, so when one timer finishes, the next one starts. One for preparation time, one for workout time and one for rest time, the user chooses the times of these.
I need it to loop however many times the user selects from a numberpicker, but no matter what I do it only goes through it once and doesn't loop so I know it all works it's just the looping part that doesn't work.
Am I missing something here? Is there a better way to do this?
//Main countdown timers loop
for(int i = 0; i <= times.getValue() + 1; i++) //times NumberPicker
{
prepCountTimer = new CountDownTimer(_finalPrep * 1000, 1000) {
public void onTick(long millisUntilFinished) {
tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
tvCountDown.setText((millisUntilFinished / 1000) + "s");
if(millisUntilFinished <= (6 * 1000))
{
tvCountDown.setTextColor(Color.RED);
}
}
public void onFinish() {
workoutCountTimer = new CountDownTimer(_finalWorkout * 1000, 1000) {
public void onTick(long millisUntilFinished) {
tvCountDown.setTextColor(Color.GREEN);
tvCountDown.setText((millisUntilFinished / 1000) + "s");
if(millisUntilFinished <= 6 * 1000)
{
tvCountDown.setTextColor(Color.RED);
}
}
public void onFinish() {
restCountTimer = new CountDownTimer(_finalRest * 1000, 1000) {
public void onTick(long millisUntilFinished) {
tvCountDown.setTextColor(Color.GREEN);
tvCountDown.setText((millisUntilFinished / 1000) + "s");
if(millisUntilFinished <= 6 * 1000)
{
tvCountDown.setTextColor(Color.RED);
}
}
public void onFinish() {
roundCount = roundCount + 1;
}
}.start();
}
}.start();
}
}.start();
}
the issue here is that you create prepCountTimer and assign on finish ect, then start it. then it reaches the end of for each and loops again making another preopCountTimer and starting it. you need to make your restCountTimer start the next preopCountTimer once it's done. unless I'm understanding something wrong here.
public void callingMethod() {
timerMethod(times.getValue());
// execution continues as your timer will run in a different thread
}
public void timerMethod(final int count) {
if (count == 0) {
// we have done the number of timers we want we can
// call whatever we wanted to once our timers were done
}
//you could use count to get the times for each timer here
startTimer(_finalPrep, new timerListner() {
#Override
public void timerFinish() {
//when timer 1 finishes we will start timer 2
startTimer(_finalWorkout, new timerListner() {
#Override
public void timerFinish() {
//when timer 2 finishes we will start timer 3
startTimer(_finalRest, new timerListner() {
#Override
public void timerFinish() {
//when timer 3 finishes we want to call the next timer in the list.
timerMethod(count - 1);
}
});
}
});
}
});
}
private interface timerListner {
void timerFinish();
}
public void startTimer(int timerTime, final timerListner onFinish) {
// you can pass in other parameters unqiue to each timer to this method aswell
CountDownTimer timer = new CountDownTimer(timerTime * 1000, 1000) {
public void onTick(long millisUntilFinished) {
tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
tvCountDown.setText((millisUntilFinished / 1000) + "s");
if (millisUntilFinished <= (6 * 1000)) {
tvCountDown.setTextColor(Color.RED);
}
}
#Override
public void onFinish() {
onFinish.timerFinish();
}
};
timer.start();
}

Categories

Resources