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 to realize countdown in Android

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "how to use CountDownTimer to achieve countdown in Android", the content is detailed, the steps are clear, the details are handled properly, I hope this article "how to use CountDownTimer in Android to achieve countdown" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

What is CountDownTimer?

CountDownTimer is a countdown tool packaged for us by Google under the Andorid.os package. We, usually in the development process like some CAPTCHA, countdown functions, if you package a countdown tool will be a little troublesome. And Google is a very convenient tool to use.

Source code

Package android.os;public abstract class CountDownTimer {public CountDownTimer (long millisInFuture, long countDownInterval) {throw new RuntimeException ("Stub!");} public final synchronized void cancel () {throw new RuntimeException ("Stub!");} public final synchronized CountDownTimer start () {throw new RuntimeException ("Stub!");} public abstract void onTick (long var1); public abstract void onFinish ();}

Constructor:

Two parameters, the first is the total countdown time, and the second is the countdown interval. For example, if you jump once per second, the parameter is 1000, and both parameters are in milliseconds.

Start ():

Call this method to start the timer.

Cancel ():

Call this method to release the timer.

important! Be sure to hit the cancel () timer in onDestory, etc., otherwise a null pointer error will occur because the page's recycled timer is still running!

Callback method:

OnTick ():

This method is called back once every time interval. For example, if the interval of 1000ms is set, this method will be called back once every 1000ms.

OnFinish ():

Callback after the end of the entire timer.

Some pits in use

The problem of inaccurate timing

Although this tool deals with delays, it is very likely that CountDownTimer will not start timing from the total length of time you set for some other reasons. For example, if you set the millsInFuture to 30000 (30s), CountDownTimer actually starts with 29xxx, which will cause some Text UI to display one second faster. The solution is to pass in a number larger than 30000, such as 30300.

Memory leak / null pointer problem

CDT actually encapsulates Handler, so be sure to call the cnacel () method of Timer during recycling, otherwise it is prone to null pointer errors.

Read this, the article "how to use CountDownTimer in Android to achieve countdown" article has been introduced, want to master the knowledge of this article still need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

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