Android 倒数计时器跳过了几毫秒
正如您所看到的,上次调用onTick的时间是2秒,而下一次调用的时间是近2秒Android 倒数计时器跳过了几毫秒,android,timer,countdown,Android,Timer,Countdown,正如您所看到的,上次调用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, H
@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);
}
}
输出:
请查看视频了解更多信息
我想在计算中有一些错误,它对我来说是这样的: [编辑]刚刚再次测试了它,它工作正常,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);
}
}
希望有帮助;) 试试这个:
TimerTask runTimerTask = new TimerTask() {
@Override
public void run() {
// your code
});
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(runTimerTask, 0, 500);
你能修改我的代码吗。。你刚刚展示了hrs,min,Sec刚刚做到了,希望现在能有所帮助;)我也需要几天时间。。请在这方面给予帮助,我相信这或多或少是你想要的;整日=(整日)((百万分之七十/(1000*60*60)*24)%7);
TimerTask runTimerTask = new TimerTask() {
@Override
public void run() {
// your code
});
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(runTimerTask, 0, 500);