倒数计时器问题
我正在使用这个 Android 类来实现倒计时:http://developer.android。 com/reference/android/os/CountDownTimer.html
我创建了一个自己的类 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);
}
}
在我的活动的 onStart() 中,我有以下代码:
MyCount counter = new MyCount(time, 1000);
counter.start();
现在的问题是:如果我导航到下一个活动并返回,倒计时开始再次进行两次倒计时。
我不能将它放在 onCreate() 中,因为时间变量可以由用户更改。他在其他活动中更改了它
I am using this Android class to implement a countdown: http://developer.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试添加一个布尔值
示例:
在计数器的实现中添加以下内容:
并在 onStart() 中尝试测试变量
,当您更改计时器时,执行如下操作:
希望它对人有帮助:)
try to add a boolean
example :
and in your implementation of the counter add this :
and in your onStart() try to test the variable
and when you changed the timer , do something like this :
hope it helps man :)