开发者

Countdown Timer problem

开发者 https://www.devze.com 2023-03-05 20:46 出处:网络
I am using this Android class to implement a countdown: http://develo开发者_Python百科per.android.com/reference/android/os/CountDownTimer.html

I am using this Android class to implement a countdown: http://develo开发者_Python百科per.android.com/reference/android/os/CountDownTimer.html

I have created an own class My Count:

public class MyCount extends CountDownTimer{

public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onFinish() {
tv_test.setText("");
}

@Override
public void onTick(long millisUntilFinished) {
tv_test.setText("d"+ (millisUntilFinished/(3600000*24))+ " h: "+  (millisUntilFinished/3600000)%24  +" m: " +  (millisUntilFinished/60000) % 60 + " sec: "+   (millisUntilFinished/1000) % 60);

}

}

in onStart() of my activity I have this code:

MyCount counter = new MyCount(time, 1000);
        counter.start();

The problem is now: If I navigate to next activity and come back, the countdowntimer is started again and there run two countdowns.

I can not put it in onCreate() because the time variable can be changed by the user. He changes it on an other activity


try to add a boolean example :

booelan counterIsLaunched = false;
boolean timeChanged = false;

and in your implementation of the counter add this :

@Override
public void onFinish() { tv_test.setText("");
counterIsLaunched = false;
if(timeChanged) start();
}
@Override
public void onTick(long millisUntilFinished) {
tv_test.setText("d"+ (millisUntilFinished/(3600000*24))+ " h: "+  (millisUntilFinished/3600000)%24  +" m: " +  (millisUntilFinished/60000) % 60 + " sec: "+   (millisUntilFinished/1000) % 60);
counterIsLaunched = true;
if(timeChanged) start();
}

and in your onStart() try to test the variable

if(counter.getCounterIsLaunched() == false ) {
counter.start();
}

and when you changed the timer , do something like this :

counter.getTimeChanged() = true;

hope it helps man :)

0

精彩评论

暂无评论...
验证码 换一张
取 消