In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "Android how to achieve the countdown function", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve the countdown function of Android" this article.
one。 Problems with the existing countdown scheme
When developing the countdown function, we often use CountDownTimer or Handler for convenience. Of course, Handler is also used in the internal encapsulation of CountDownTimer.
It doesn't matter if you just do a small countdown or don't need precise countdown logic, for example, I only need a 10-second countdown, or I request an interface in about 5 minutes.
But if you need to do an accurate countdown, such as sending a CAPTCHA for 60 seconds, there will be problems using the existing countdown scheme. Some friends may not have noticed this, so let's briefly analyze the existing countdown.
1. CountDownTimer
This is probably the most frequently used, because it is convenient. But in fact, after each round of countdown, there is an error. If you have seen the source code of CountDownTimer, you will know that his internal calibration operation is done. (the source code is very simple, so I won't analyze it here.)
But if you seriously test the CountDownTimer, you will find that even if there is an internal calibration operation, there is no deviation in every round, except that the total time after his last countdown is the same as the time he started the countdown.
What do you mean, it means 1 second, 2.050 seconds, 3.1s. This kind of deviation in each round will cause him to have a situation of 10.95 seconds, and the next 12 seconds, then there will be one second less in its callback if you directly round it up, but there is actually no less.
This is just one of the problems, and you can solve it by yourself instead of making a presentation based on its callback. But he also has a problem, there is a probability of direct jump seconds, such as 3 seconds, the next time directly 5 seconds, this is the actual jump seconds, is the kind of missing a callback.
Skip seconds may cause big problems if you use it directly, you may not find it when you test yourself, and when you go online and apply it to users, the probability of skip seconds will hurt.
2. Handler
What's the problem if you don't do so much fancy stuff and just use Handler to achieve it.
Because it is directly realized by using handler, there is no calibration operation, and each cycle will have an error of several milliseconds. Although it is better than the error of more than ten milliseconds of CountDownTimer, the error will accumulate in the case of a large base countdown, resulting in an error of several seconds between the final result and the real time. The longer the time, the greater the error.
3. Timer
It is the same to use Timer directly, except that the error of each round is smaller, only 1 millisecond for several rounds, but there will still be error accumulation without calibration, and the longer the time, the greater the error.
two。 Self-encapsulated countdown
Since we can't use the native one directly, we'll make one ourselves.
We encapsulate based on Handler, from which we can see that the main purpose is to solve two problems, time calibration and second hopping. Write your own CountDownTimer.
Public class CountDownTimer {private int mTimes; private int allTimes; private final long mCountDownInterval; private final Handler mHandler; private OnTimerCallBack mCallBack; private boolean isStart; private long startTime; public CountDownTimer (int times, long countDownInterval) {this.mTimes = times; this.mCountDownInterval = countDownInterval; mHandler = new Handler ();} public synchronized void start (OnTimerCallBack callBack) {this.mCallBack = callBack If (isStart | | mCountDownInterval mCountDownInterval) {mTimes -; if (mCallBack! = null) {mCallBack.onTick (mTimes);} delay-= mCountDownInterval; if (mTimes)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.