类倒数计时器不会重置

时间:2018-03-13 13:33:03

标签: java android android-studio countdowntimer

我有一个我想要重置的类倒数计时器但是当它重置时它不会重新启动。 当计数器到达0时(当状态为“已完成”时),设置x = 1,然后在活动中检查是否x = 1,然后计数器重置。当调用类中的reset方法时,它表明它已经重置但它不会再次开始计数

计时器课程:

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);
}

使用计时器类的活动:

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

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

尝试更改您的启动方法,还有一件事我不知道它是否是一个错字或更改[Route("api/[controller]")] public class ValuesController : Controller { // GET api/values [HttpGet] public IEnumerable<string> Get() { return new string[] {"hello", "world!"}; } // POST api/values [HttpPost] public void PostCreate([FromBody] string value) { } } mycounter.Start();

mycounterup.Start();

保存计数器状态,以便下次如果你必须暂停或恢复你能够做的事情

答案 1 :(得分:1)

您应该更改Reset方法:

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