倒计时器跳过几毫秒

时间:2015-06-19 14:16:19

标签: android timer countdown

你可以看到最后一次打电话onTick正在进行2秒,然后下一次打电话将近2秒。

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);
    }
}

CountDownTimer

10 seconds 
8 seconds 
6 seconds 
4 seconds 
  

输出:

$(DefaultFilePath)

P.S~查看视频了解更多信息

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

2 个答案:

答案 0 :(得分:4)

试试这个:

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

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

答案 1 :(得分: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);
    }
}

希望它有所帮助;)