2012-10-25 27 views
0

我正在做一個倒數計時器。我已經想出了這個代碼,最初我認爲它的工作。然而,當我運行的應用程序發生兩件事情,我不知道如何解決......Android中的倒數計時器不同步?

  1. 這是不同步的,雖然它是系統時間和日期它說只有60天了不存在61,當我知道它是61!
  2. 如果我關閉應用程序,並返回其重置計數器...

任何幫助,這將不勝感激,它不是什麼特別的事情只是一個個人項目。

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final TextView dateBx = (TextView) findViewById(R.id.textView1); 
     final TextView mTextField = (TextView) findViewById(R.id.mTextField); 
     final TextView minBx = (TextView) findViewById(R.id.minBx); 
     final TextView hourBx = (TextView) findViewById(R.id.hourBx); 

     //set todays date 
     DateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yyyy"); 
     Calendar cal1 = Calendar.getInstance(); 
     //target date 
     DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy"); 
     Calendar cal2 = Calendar.getInstance(); 
     cal2.set(2012,11,25); 
     //set maths 
     long time1 = cal1.getTimeInMillis(); 
     long time2 = cal2.getTimeInMillis(); 
     //difference variable 
     long diff = time2 - time1; 
     //equations 
     long diffSec = diff/1000; 
     long dMins = diff/(60 * 1000); 
     long dHour = diff/(60 * 60 * 1000); 
     long dDay = diff/(24 * 60 * 60 * 1000); 

     new CountDownTimer(diff, 1000) 
     { 

      public void onTick(long millisUntilFinished) 
      { 
       mTextField.setText("Seconds remaining: " + millisUntilFinished/1000); 
       dateBx.setText("Days remaining: " + millisUntilFinished/(24 * 60 * 60 * 1000)); 
       minBx.setText("Minutes remaining: " + millisUntilFinished/(60 * 1000)); 
       hourBx.setText("hours remaining: " + millisUntilFinished/(60 * 60 * 1000)); 
      } 

      public void onFinish() { 
       mTextField.setText("done!"); 
      } 
      }.start(); 



} 

我的代碼的一些東西,我學到了組合,在Java和Android,如果你想這樣做它一起的更好的方法,那麼請讓我知道:)

感謝

+0

你能提供一個例子,說明日期和時間錯誤的日期? – Sam

+0

我分配給文本字段的變量與時鐘不同步,這就是我遇到的問題 – clearcut89

回答

0

Android不是一個實時操作系統,因此您可以指望您的計時器精確地每1000毫秒運行一次。

最實用的方法是,對於每次調用,獲取當前日期,獲取目標日期,並重新計算剩餘的日/小時/分鐘/秒(就像您的應用程序的開始一樣)。

這也將解決您的問題時,關閉/打開你的應用程序。

+0

我一直在努力解決你的意思,我無法弄清楚,任何可以顯示的例子?或代碼你顯示它將去的地方的佈局,我可以從那裏解決它,除此之外,我知道你的意思,我只是不知道如何解決它... – clearcut89

+0

而不是在每次調用時將'diff'傳遞給您的計時器並減去'1000',每次重新計算'diff'(從頭開始)。這樣你的鬧鐘不會失去同步。 – SJuan76

+0

嗨,對不起,我想我是有點密集!我沒有完全理解你的意思,我玩過它,我似乎無法理解你的意思?你有什麼機會展示一些例子?謝謝 – clearcut89

1

以下是如何同步countDownTimer的示例。

訣竅是隻使用「onFinish()」,並使用接口開始一個新的計時器(爲「onTick()」根據Android是不準確)。

使countDownTimer的擴展類:

public class oneShotTimer extends CountDownTimer { 
    TickListener invokeOnFinish; // Interface 

public oneShotTimer (Context parent, int tickTime, TickListener contextPointer) { 
    super(tickTime, tickTime);   // DEBUG: (/60) 
.... 
} 

在OnFinish():

public void onFinish() { 
    invokeOnFinish.restartTimer();  
} 
public void onTick() { 
    Log.e("Timer", "Not suppose to happen"); 
} 

定義一個接口:在父對象

public interface TickListener { 
    public void restartTimer(); 
} 

做如下:

1)在啓動定時器之前,獲取MillinSecond的時間 - >「startTime」;

2)創建 「TickListener :: restartTimer」 的方法(我寫psadu代碼)

@Override 
public void restartTimer() { 
    // check if timer is done (make your own variable to monitor that) 

    // get the currentTime in MilliSecond 

    // calc TickTime , if we know that 30 tick are already counted 
    // there for ((30+1) x wantedTickTime) give us the target time for the next tick. 
    // TickTime = (31 x wantedTickTime) - (currentTime - startTime) 
    // for example, we want 1sec ticks, after 30 ticks it finished at 30.2 sec 
    // => (31 x 1) - (40.2 - 10) = 0.8 is the time we need to re start timer. 

    // re-run a new Timer with the new TickTime 
} 

現在,讓我們看看Java垃圾收集將如何處理它。