本文主要是介绍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();}
这篇关于CountDownTimer倒计时使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!