在這裏,我有這個CountDownTimer我想開始並重新啓動一個無限循環,我不知道爲什麼它不工作:無盡CountdownTimer
prefs = PreferenceManager.getDefaultSharedPreferences(this);
xtime = System.currentTimeMillis()-prefs.getLong("time",System.currentTimeMillis());
timer=24000-(xtime+prefs.getLong("time2",0));
final SharedPreferences.Editor editor = prefs.edit();
editor.putLong("time2",xtime+prefs.getLong("time2",0));
editor.commit();
new CountDownTimer(timer, 1000) {
public void onTick(long elapsed) {
long timer2=elapsed;
hours = timer2/hours_in_millies;
timer2 %= hours_in_millies;
minutes = timer2/minutes_in_millies;
timer2 %= minutes_in_millies;
Log.v(TAG, "seconds" + seconds);
if(seconds<10)
seconds=timer2/1000;
else
seconds = timer2/seconds_in_millies;
if(seconds<10)
seconds=timer2/100;
if(seconds>1&&seconds<2)
seconds=0;
if(hours>=10&&minutes>=10&&seconds>=10)
time.setText(hours + ":" + minutes + ":" + seconds);
else
if (hours<10&&minutes>=10&&seconds>=10)
time.setText("0"+hours + ":" + minutes + ":" + seconds);
else
if (hours<10&&minutes<10&&seconds>=10)
time.setText("0"+hours + ":0" + minutes + ":" + seconds);
else
if (hours>=10&&minutes<10&&seconds<10)
time.setText("0"+hours + ":0" + minutes + ":0" + seconds);
else
if(hours>=10&&minutes>=10&&seconds<10)
time.setText(hours + ":" + minutes + ":0" + seconds);
else
if (hours>=10&&minutes<10&&seconds<10)
time.setText(hours + ":0" + minutes + ":0" + seconds);
else
if(hours>=10&&minutes<10&&seconds>=10)
time.setText(hours + ":0" + minutes+":" + seconds);
else if(hours<10&&minutes>=10 && seconds<10)
time.setText("0"+hours + ":" + minutes +":0"+ seconds);
}
public void onFinish()
{ editor.clear();
this.start();
}
}.start();
xtime=System.currentTimeMillis();
SharedPreferences.Editor editor2 = prefs.edit();
editor2.putLong("time",xtime);
editor2.commit();
}
的SharedPreferences幫助我從使用我的算法結束活動的角度恢復CountDownTimer。問題是,即使我在我的OnFinish()中調用this.start(),我在this.start的行上有這個NullPointerException錯誤。我希望得到一些幫助。謝謝! P.S:當計時器低於10秒時,我也有問題,之後出現以下數字,但有獎金數字。 (95,85,75 ...... 15)。如果我:
if(seconds<10)
seconds=timer2/1000;
else
seconds = timer2/seconds_in_millies;
我會改變秒=定時器/ 1000定時器/ 10000它不顯示在所有的數字,並在10
,我相信你的代碼是好的,我需要知道如何實現它(我不是很先進Android開發)。那麼,我需要在我的代碼中複製這個My Count類,然後呢?另外,我不需要知道總時間,所以我將刪除該部分。 – AnTz 2012-03-16 10:39:10
在你的活動..創建後粘貼這個MyCount類..然後在oncreate活動的方法..把這個代碼..我的計數器; counter = new MyCount(30000,1000);; counter.start(); – 5hssba 2012-03-16 10:42:29
我應該怎樣處理我的SharedPreferences,他們需要被刪除才能重新啓動計時器。 – AnTz 2012-03-16 10:44:44