2012-03-16 103 views
0

在這裏,我有這個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

回答

1

你可以做到這一點..

您oncreate

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 

Date date = (Date)settings.getString("date",0); 
Calendar c = Calendar.getInstance(); 
     Date d=c.getTime(); 
long diffInMs = d.getTime() - date.getTime(); 
long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMis); 
if (diffInSec>86400) 
{ 
SharedPreferences myPrefs = this.getSharedPreferences("PREFS_NAME",0); 
SharedPreferences.Editor ed = myPrefs.edit(); 
ed.putString("Date", String.valueOf(d)); 
ed.commit(); 
    //allow your activity to open 
} 
else 
{ 
counter= new MyCount(diffInSec*1000,1000); 
counter.start(); 
} 

mycount的類

public class MyCount extends CountDownTimer{ 
public MyCount(long millisInFuture, long countDownInterval) { 
super(millisInFuture, countDownInterval); 
} 
@Override 
public void onFinish() { 
    Intent k=new Intent(Myactivity.this,Myactivity.class); 
       startActivity(k); 
} 
@Override 
public void onTick(long millisUntilFinished) { 
    s1=millisUntilFinished; 
    <---- display s1/1000 to show number of seconds left 

} 
} 

}

希望這個作品..

1

這給你無盡的倒數計時器停止。 30秒的..你可以指望有多少次新的計時器已經建立,以獲得經過總時間..這樣

MyCount counter; 
counter=new MyCount(30000,1000); 
    counter.start(); 

mycount的類

public class MyCount extends CountDownTimer{ 
    public MyCount(long millisInFuture, long countDownInterval) { 
    super(millisInFuture, countDownInterval); 
    } 
    @Override 
    public void onFinish() { 
     counter= new MyCount(30000,1000); 
    counter.start(); 
    n=n+1; 
    } 
    @Override 
    public void onTick(long millisUntilFinished) { 
     s1=millisUntilFinished; 
     r1=(30000-s1)/1000; 


    } 
    } 



    } 

得到總時間流逝......

int time=(n*30)+r1;<--- total time elapsed.. 

暫停計時器使用

counter.cancel(); 

恢復使用

counter= new MyCount(s1,1000); 
    counter.start(); 
+0

,我相信你的代碼是好的,我需要知道如何實現它(我不是很先進Android開發)。那麼,我需要在我的代碼中複製這個My Count類,然後呢?另外,我不需要知道總時間,所以我將刪除該部分。 – AnTz 2012-03-16 10:39:10

+0

在你的活動..創建後粘貼這個MyCount類..然後在oncreate活動的方法..把這個代碼..我的計數器; counter = new MyCount(30000,1000);; counter.start(); – 5hssba 2012-03-16 10:42:29

+0

我應該怎樣處理我的SharedPreferences,他們需要被刪除才能重新啓動計時器。 – AnTz 2012-03-16 10:44:44