2011-10-04 65 views
2

我有一個天文臺。應該顯示時間。當我開始計時器的時間總是相同的,但它應該更新天文臺更新時間

呼叫:

   Log.e("XXX","OnClick"); 
       String[] array = clock.getText().toString().split(":"); 
       int ms = (Integer.valueOf(array[0])*60 + Integer.valueOf(array[1]))*60000; 
       Log.e("XXX",String.valueOf(ms)); 
       MyTimer counter = new MyTimer(ms, 1000, Timer.this,clock); 

       counter.start(); 

定時器:

public class MyTimer extends CountDownTimer { 
    private Context con; 
    private Chronometer clock; 

    public MyTimer(long millisInFuture, long countDownInterval, Context context, Chronometer clock) { 
     super(millisInFuture, countDownInterval); 
     this.con = context; 
     this.clock= clock; 
     // TODO Auto-generated constructor stub 
    } 


    @Override 
    public void onTick(long millisUntilFinished) { 

     int seconds = (int) (millisUntilFinished/1000) % 60 ; 
     int minutes = (int) ((millisUntilFinished/(1000*60)) % 60); 
     int hours = (int) ((millisUntilFinished/(1000*60*60)) % 24); 
     clock.setText(String.format("%02d:%02d:%02d", hours,minutes,seconds)); 
     Log.e("Timer", String.valueOf(millisUntilFinished)); 

    } 

的問題是,在天文鐘的時間總是相同。並且日誌(定時器) 也不會出現。難道是UI的刷新過快?

+0

運行此代碼時'ms'的值是多少?它是'> 1000'嗎? – Caner

+0

耶至少1分鐘= 60000ms – test123123

+0

你確定你沒有得到異常的地方嗎?順便嘗試將整個代碼放入'try/catch'中,以便測試 – Caner

回答

0

對於將您的上下文和視圖作爲擴展類的參數進行傳遞,我並不十分有信心。也許你應該嘗試這種適應你的代碼(並擺脫你的MyTimer類)?

Log.e("XXX","OnClick"); 
String[] array = clock.getText().toString().split(":"); 
int ms = (Integer.valueOf(array[0])*60 + Integer.valueOf(array[1]))*60000; 
Log.e("XXX",String.valueOf(ms)); 

new CountDownTimer(ms,1000){ 
    @Override 
    public void onTick(long millisUntilFinished) { 

     int seconds = (int) (millisUntilFinished/1000) % 60 ; 
     int minutes = (int) ((millisUntilFinished/(1000*60)) % 60); 
     int hours = (int) ((millisUntilFinished/(1000*60*60)) % 24); 
     clock.setText(String.format("%02d:%02d:%02d", hours,minutes,seconds)); 
     Log.e("Timer", String.valueOf(millisUntilFinished)); 

    } 
}.start(); 
+0

@ test123123你能確認它正在工作嗎?謝謝。 –