public CountTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void initData() {
countDownTimer = new CountDownTimer(200000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
//单位天
long day = millisUntilFinished / (1000 * 24 * 60 * 60);
//单位时
long hour = (millisUntilFinished - day * (1000 * 24 * 60 * 60)) / (1000 * 60 * 60);
//单位分
long minute = (millisUntilFinished - day * (1000 * 24 * 60 * 60) - hour * (1000 * 60 * 60)) / (1000 * 60);
//单位秒
long second = (millisUntilFinished - day * (1000 * 24 * 60 * 60) - hour * (1000 * 60 * 60) - minute * (1000 * 60)) / 1000;
txtRemainingTime.setText(“剩余时间:” + getTv(hour) + “:” + getTv(minute) + “:” + getTv(second));
}
/**
倒计时结束后调用的
/
@Override
public void onFinish() {
}
};
countDownTimer.start();
}
/
- 关闭,避免内存溢出
*/
@Override
protected void onDestroy() {
super.onDestroy();
if (countDownTimer != null) {
countDownTimer.cancel();
countDownTimer = null;
}
}
//十位数不够 补零
private String getTv(long l) {
if (l >= 10) {
return l + “”;
} else {
return “0” + l;//小于10,前面补位一个"0"
}
}