CountDownTimer倒计时使用
CountDownTimer倒计时使用
- CountDownTimer
- 使用
CountDownTimer
代码片
.
// An highlighted blockprivate MyCountDownTimer timer;private final long TIME = 7 * 1000L;private final long INTERVAL = 1000L;private class MyCountDownTimer extends CountDownTimer{/*** @param millisInFuture The number of millis in the future from the call* to {@link #start()} until the countdown is done and {@link #onFinish()}* is called.* @param countDownInterval The interval along the way to receive* {@link #onTick(long)} callbacks.*/public MyCountDownTimer(long millisInFuture, long countDownInterval) {super(millisInFuture, countDownInterval);}@Overridepublic void onTick(long millisUntilFinished) {long time = millisUntilFinished / 1000;Log.e("wuzhang",time+"秒");}@Overridepublic void onFinish() {cancelTimer();adsAPI.startAds(QRSuccessActivity.this, adRequest);finish();}}/*** 开始倒计时*/private void startTimer() {if (timer == null) {timer = new MyCountDownTimer(TIME, INTERVAL);}timer.start();}/*** 取消倒计时*/private void cancelTimer() {if (timer != null) {timer.cancel();timer = null;}}
使用
代码片
.
@Overrideprotected void onCreate(Bundle savedInstanceState) {startTimer();.......
}@Overrideprotected void onDestroy() {super.onDestroy();cancelTimer();}