[英]Why doesn't countdown timer work on standalone class which is not an activity?
[英]Class countdown timer doesn't reset
我有一個類倒計時器,我想重置,但當它重置它不會再啟動。 當計數器變為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();
謝謝你的幫助。
嘗試改變你的啟動方法作為一件事我不知道它是一個錯字或什么改變mycounter.Start();
到 mycounterup.Start();
public void Start() {
status = true;
Initialize();
}
保存計數器狀態,以便下次如果你必須暫停或恢復你可以做的事情
你應該改變你的Reset
方法:
public void Reset() {
millisInFuture = pls;
x=0;
Initialize();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.