In development, the countdown function is often used, including sending verification codes to mobile phones, etc. My previous practice was to use Handler + Timer + TimerTask to implement it. Now that I have discovered this class, I have decisively abandoned my previous practices. I believe that many people, like me, didn’t know at the beginning that Android had helped us encapsulate a class called CountDownTimer.
From the literal perspective, it can be seen that it is called a countdown timer, also known as a timer or timer, and is implemented in the form of a Handler, encapsulating the creation of background threads and the Handler queue.
After looking at the source code, I found that the call of this class is quite simple, with only four methods:
(1)public abstract void onTick(long millisUntilFinished);
Fixed interval is called
(2)public abstract void onFinish();
Called when the countdown is completed
(3)public synchronized final void cancel():
Cancel the countdown, and the countdown will restart when restarted
(4)public synchronized final CountDownTimer start():
Start countdown
Here we can see that the first two are abstract methods and need to be rewritten.
A brief look at the code:
package ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { private TextView mTvShow; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); mTvShow = (TextView) findViewById(); } /** * Cancel countdown * @param v */ public void oncancel(View v) { (); } /** * Start countdown * @param v */ public void restart(View v) { (); } private CountDownTimer timer = new CountDownTimer(10000, 1000) { @Override public void onTick(long millisUntilFinished) { ((millisUntilFinished / 1000) + "Repost in seconds"); } @Override public void onFinish() { (true); ("Get verification code"); } }; }
Attach XML layout file
<LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical" android:padding="16dp"> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:onClick="restart" android:text="Cancel" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:onClick="oncancel" android:text="Finish" /> </LinearLayout>
Finally, let me explain:
CountDownTimer timer = new CountDownTimer(10000, 1000): In milliseconds, the first parameter refers to the number of milliseconds during which the onFinish() method is called from the start() method to the completion of the countdown, which is the total countdown time; the second parameter indicates how many milliseconds are called once the onTick method, such as 1000 milliseconds.
Use () directly when calling;
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.