2015-06-19 179 views
2

正如您可以看到最後一次呼叫onTick正在發生2秒鐘,然後接下來的呼叫將近2秒鐘後。倒計時器跳過一些毫秒

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

tv = new TextView(this); 
this.setContentView(tv); 
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss:SSS"); 
formatter.setLenient(false); 


newTime = "31.12.2015, 23:59:59:999"; 

try { 
    newDate = formatter.parse(newTime); 
    milliseconds = newDate.getTime(); 


    currentTime = System.nanoTime(); 

    diff = milliseconds - currentTime; 


} catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

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


} 

CountDownTimer

public class MyCount extends CountDownTimer { 
    public MyCount(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
    } 

    @Override 
    public void onFinish() { 
    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     serverUptimeSeconds = (millisUntilFinished - System 
       .currentTimeMillis())/1000; 
     String serverUptimeText = String.format(
       "%d days %d hours %d minutes %d seconds", 
       serverUptimeSeconds/86400, 
       (serverUptimeSeconds % 86400)/3600, 
       ((serverUptimeSeconds % 86400) % 3600)/60, 
       ((serverUptimeSeconds % 86400) % 3600) % 60); 
     tv.setText(serverUptimeText); 
    } 
} 

輸出:

10 seconds 
8 seconds 
6 seconds 
4 seconds 

P.S〜檢查視頻更多信息

https://www.dropbox.com/s/7b3hme5ekdqpfi1/79789789.avi?dl=0

回答

4

嘗試了這一點:

TimerTask runTimerTask = new TimerTask() { 
    @Override 
    public void run() { 
     // your code 
    }); 
}; 

Timer timer = new Timer(); 
timer.scheduleAtFixedRate(runTimerTask, 0, 500); 
0

我想有在計算一些錯誤,這是爲我工作是這樣的:

[編輯]就在今年再次測試,其做工精細,更新1在數秒內

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

     tv = new TextView(this); 
     this.setContentView(tv); 
     SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss:SSS"); 
     formatter.setLenient(false); 


     String newTime = "31.12.2015, 23:59:59:999"; 

     long diff = 0; 
     try { 
      Date newDate = formatter.parse(newTime); 
      milliseconds = newDate.getTime(); 


      long currentTime = System.currentTimeMillis(); 

      diff = milliseconds - currentTime; 


     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     MyCount counter = new MyCount(diff, 1000); 
     counter.start(); 
    } 

我看到你也使用納秒,請嘗試使用毫秒。

public class MyCount extends CountDownTimer { 
    private long serverUptimeSeconds; 

    public MyCount(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
    } 

    @Override 
    public void onFinish() { 
    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     //serverUptimeSeconds = (millisUntilFinished - System 
     //  .currentTimeMillis())/1000; 

     int seconds = (int) (millisUntilFinished/1000) % 60 ; 
     int minutes = (int) ((millisUntilFinished/(1000*60)) % 60); 
     int hours = (int) ((millisUntilFinished/(1000*60*60)) % 24); 

     String serverUptimeText = String.format(
       "%d hours %d minutes %d seconds", 
       hours, 
       minutes, 
       seconds); 
     tv.setText(serverUptimeText); 
    } 
} 

希望它幫助;)

+0

你能在我的代碼修改..烏爾只是顯示小時,分鐘,秒 – Gattsu

+0

只是做了它,希望它有助於現在;) – iGoDa

+0

我需要幾天也..在這部分plz幫助 – Gattsu