java - 类(class)倒数计时器不会重置

标签 java android android-studio countdowntimer

我有一个类(class)倒数计时器,我想重置它,但是当它重置时它不会再次开始。 基本上当计数器到达 0(当状态为“完成”时),设置 x = 1 然后在 Activity 中检查是否 x = 1 ,然后计数器重置。当类中的重置方法被调用时,它显示它已重置但不会再次开始计数

定时器类:

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 :

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

感谢您的帮助。

最佳答案

尝试改变你的开始方法,还有一件事我不知道它是一个错字还是什么改变了 mycounter.Start(); to mycounterup .开始();

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

保存counter状态,这样下次如果你必须暂停或恢复你也能做的事情

关于java - 类(class)倒数计时器不会重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49257535/

相关文章:

javascript - Android 中的自定义 YouTube 播放器

android-studio - Gradle构建工具版本在哪里设置?

android - 如何使用android Wear DataApi发送相同的数据两次?

java - 使用 Gradle 版本 2.2.3 在 android studio 项目中无法识别方法 testCompile?

java - Java 客户端应用程序中的 Ajax 调用

java - 添加库为 "Referenced library"

java - 我应该如何将功能划分为 Java 类?

java - 为什么 sbt 程序集在更新依赖项时会抛出错误?

android - 如何在 Libgdx 中更改 AdMob 方向

android - 仅更新 sqlite 列值的特定部分