Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CountDownTimer class in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the CountDownTimer class in Android. It is very detailed and has a certain reference value. Friends who are interested must finish it!

I. Overview

Countdown functions are often used in projects, such as time-limited rush purchase, mobile phone access to CAPTCHA, and so on. And google officially sealed a class for us: CountDownTimer, which makes our development more convenient.

II. API

CountDownTimer is an abstract class with two abstract methods, and its API is very simple

Public abstract void onTick (long millisUntilFinished); / / this is the callback for each specified time interval. MillisUntilFinished: the remaining time (in milliseconds public abstract void onFinish ()); / / this is the callback at the end of the countdown.

When you use it, all you need is

New CountDownTimer (long millisInFuture, long countDownInterval) / / millisInFuture: total length of countdown / / countDownInterval: each interval is in milliseconds. Basic usage

From the point of view of the countdown to the SMS verification code, click to get the verification code. The countdown time is 60 seconds.

New CountDownTimer (60 * 1000, 1000) {@ Override public void onFinish () {if (tvCode! = null) {tvCode.setText ("reacquire"); tvCodeWr.setTextColor (Color.parseColor ("# E94715")); tvCode.setClickable (true); tvCode.setEnabled (true);} cancel () } @ Override public void onTick (long millisUntilFinished) {if (tvCode! = null) {tvCode.setClickable (false); tvCode.setEnabled (false); tvCode.setText (millisUntilFinished / 1000 + "s"); tvCode.setTextColor (Color.parseColor ("# 999999"));}} .start ()

Click the button to obtain the verification code successfully, and then you can perform the above operations. Finally, you must start, otherwise it will not be executed.

Fourth, pay attention to the use

CountDownTimer is easy to use, but there are many holes, so you need to be careful to avoid stepping on them.

1. Null pointer:

If the cancle method is not called when activity or fragment shuts down and destroys, its onTick method will continue to execute. At this time, the UI control is empty. If you do not pay attention to judgment, it is easy to empty the pointer.

2. The timing is not too accurate:

When we look at the source code of CountDownTimer, we can see that when executing the onTick method, the google source code subtracts the time spent when the program is executed here. Here we can see the rigor of the google code.

Final long millisLeft = mStopTimeInFuture-SystemClock.elapsedRealtime (); if (millisLeft

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report