Countdown program after Android obtains the verification code

Source: Internet
Author: User

Countdown program after Android obtains the verification code

During development, you will often encounter text message Verification Code retrieval. After obtaining the verification code, you need to wait for a one-minute countdown. This means you cannot send text message requests again. This requires a countdown program.

Here I encapsulated an Util class, hoping to help developers,

 

Public class TimeCountUtil extends CountDownTimer {
Private Activity mActivity;
Private Button btn; // Button

// In this constructor, You need to input three parameters: Activity, millisInFuture, countDownInterval, and the button on which you perform this operation, just upload the button.
Public TimeCountUtil (Activity mActivity, long millisInFuture, long countDownInterval, Button btn ){
Super (millisInFuture, countDownInterval );
This. mActivity = mActivity;
This. btn = btn;
}


@ SuppressLint ("NewApi ")
@ Override
Public void onTick (long millisUntilFinished ){
Btn. setClickable (false); // you cannot click
Btn. setText (millisUntilFinished/1000 + "resend after seconds"); // set the countdown time

// When the set button is gray, you cannot click it.
Btn. setBackground (mActivity. getResources (). getDrawable (R. drawable. bg_duck_back ));
Spannable span = new SpannableString (btn. getText (). toString (); // get the text of the button
Span. setSpan (new ForegroundColorSpan (Color. RED), 0, 2, Spannable. SPAN_INCLUSIVE_EXCLUSIVE); // The countdown time is RED.
Btn. setText (span );

}


@ SuppressLint ("NewApi ")
@ Override
Public void onFinish (){
Btn. setText ("re-obtain Verification Code ");
Btn. setClickable (true); // obtain the Click again
Btn. setBackground (mActivity. getResources (). getDrawable (R. drawable. bg_btn_back); // restore the background color

}


}

Call Method

 

 

Then you can use this method to create an object and call start ();

CountUtil timeCountUtil = new TimeCountUtil (this, 60000,100 0, verficationBtn );
TimeCountUtil. start ();

// Obtain the verification code
GetVerificationCode (phoneNum );

Implemented

Is it very easy, hope to help friends!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.