倒计时器跳过一些毫秒

问题描述:

正如您可以看到最后一次呼叫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

尝试了这一点:

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

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

我想有在计算一些错误,这是为我工作是这样的:

[编辑]就在今年再次测试,其做工精细,更新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