![](/img/trans.png)
[英]android CountDownTimer - additional milliseconds delay between ticks
[英]countdowntimer skipping some milliseconds
如您所见,上一次呼叫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();
}
倒计时器
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
PS〜查看视频以获取更多信息
试试看:
TimerTask runTimerTask = new TimerTask() {
@Override
public void run() {
// your code
});
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(runTimerTask, 0, 500);
我想计算上有一些错误,它对我来说是这样的:
[编辑]再次对其进行了测试,并且工作正常,在1秒钟内更新了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);
}
}
希望能帮助到你 ;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.