简体   繁体   English

类倒数计时器不会重置

[英]Class countdown timer doesn't reset

I have a class countdown timer that I want to reset but when it resets it wont start again. 我有一个类倒计时器,我想重置,但当它重置它不会再启动。 Basicaly when the counter gets to 0 (when the status is "done"), sets x = 1 and then in the activity checks if x = 1 , and then the counter resets. 当计数器变为0 (当状态为“完成”时)时,基本上设置x = 1 ,然后在活动中检查x = 1 ,然后计数器重置。 When the reset method from within the class is called it shows that it has reset but it wont start counting again 当调用类中的reset方法时,它表明它已经重置但它不会再次开始计数

Timer class: 定时器类:

public class countdown_timer {
private long pls;
private long millisInFuture;
private long countDownInterval;
private boolean status;
int x = 0;
public countdown_timer(long pMillisInFuture, long pCountDownInterval) {
    this.millisInFuture = pMillisInFuture;
    this.countDownInterval = pCountDownInterval;
    this.pls = pMillisInFuture;
    status = false;
    Initialize();
}
    public void Stop() {
    status = false;
}
    public int GetNumberX() {
    return x;
}

public void Reset() {
    millisInFuture = pls;
    x=0;
}
    public void Start() {
    status = true;
}

public void Initialize() {
    final Handler handler = new Handler();
    Log.v("status", "starting");
    final Runnable counter = new Runnable() {

        public void run() {
            long sec = millisInFuture / 1000;
            if (status) {
                if (millisInFuture <= 0) {
                    Log.v("status", "done");
                    x = 1;
                } else {
                    Log.v("status", Long.toString(sec) + " seconds remain");
                    millisInFuture -= countDownInterval;
                    handler.postDelayed(this, countDownInterval);
                }
            } else {
                Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
                handler.postDelayed(this, countDownInterval);
            }
        }
    };

    handler.postDelayed(counter, countDownInterval);
}

Activity that uses the timer class: 使用计时器类的活动:

mycounterup = new countdown_timer(startcard, 1000);
xup = mycounterup.GetNumberX();
if (xup == 1) {
   mycounterup.Reset();
   mycounterup.Start();

Thanks for any help. 谢谢你的帮助。

try changing your start method as and one thing more i don't know if its a typo or what change mycounter.Start(); 尝试改变你的启动方法作为一件事我不知道它是一个错字或什么改变mycounter.Start(); to mycounterup.Start(); mycounterup.Start();

 public void Start() {
 status = true;
 Initialize();
}

save the counter state so that next time if you have to pause or resume the thing you are able to do it also 保存计数器状态,以便下次如果你必须暂停或恢复你可以做的事情

You should change your Reset method: 你应该改变你的Reset方法:

public void Reset() {
    millisInFuture = pls;
    x=0;
    Initialize();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM