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);
Related
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
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();
}
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();
I have strange problem with countdowntimer. Normally it works well, but sometimes (like 5% chance or so) after locking phone and unlocking it bugs like on video:
https://www.dropbox.com/s/1nbp63gmv4spzgf/bug.mp4
Here i set the timer:
private void startCurrentLessonTimer() {
if(lessonsBreak) {
lessonsBreakEnd.setText(String.format("%02d:%02d:%02d", (currentLessonTimeInMillis / 1000) / 3600, ((currentLessonTimeInMillis / 1000) % 3600) / 60,
(currentLessonTimeInMillis / 1000) % 60));
} else {
currentLessonEnd.setText(String.format("%02d:%02d:%02d", (currentLessonTimeInMillis / 1000) / 3600, ((currentLessonTimeInMillis / 1000) % 3600) / 60,
(currentLessonTimeInMillis / 1000) % 60));
}
currentLessonTimer = new CountDownTimer(currentLessonTimeInMillis, 1000) {
long hours = (currentLessonTimeInMillis / 1000) / 3600;
long minutes = ((currentLessonTimeInMillis / 1000) % 3600) / 60;
long seconds = (currentLessonTimeInMillis / 1000) % 60;
#Override
public void onTick(long millisUntilFinished) {
if(seconds == 0) {
seconds = 59;
if(minutes == 0 && hours > 0) {
minutes = 59;
hours--;
}
else {
minutes--;
}
} else {
seconds--;
}
if(lessonsBreak) {
lessonsBreakEnd.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
} else {
currentLessonEnd.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
}
#Override
public void onFinish() {
getNextLesson();
getCurrentLesson();
}
}.start();
}
And this function (inside getData()) is called in onResume()
#Override
public void onResume() {
super.onResume();
getData();
}
onPause():
#Override
public void onPause() {
super.onPause();
clean();
}
clean():
private void clean() {
if(currentLessonTimer != null) {
currentLessonTimer.cancel();
currentLessonTimer = null;
}
}
I tried to use other countdowntimer, but this problem still occurs (but not that often).
Code: http://pastebin.com/nKsasJ1S
Any ideas?
It looks like you get multiple CountDownTimers refreshing your UI. This leads me to your clean() function like it's not doing it's job on every onPause(). So on next onResume() there will be multiple CountDownTimers.
I don't know how CountDownTimer works but the thing I can think of is that your CountDownTimer is start()ed but still not initialized (currentLessonTimer is still null) when onPause() is occurring not letting clean() to run so it'd be a race condition that could be avoided with a Service for example.
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();
}