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

Analysis of Android countdown artifact

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

Share

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

This article introduces the relevant knowledge of "Android countdown artifact analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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.

This is the end of the content of "Android Countdown artifact Analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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