Android get verification code after Countdown program

Source: Internet
Author: User

In development is often encountered to get SMS verification code, then get verification code after need to wait 1 minutes countdown, it is not possible to send SMS request again, this is required a countdown program

Here I encapsulated a util class, hoping to develop a small partner can help,


public class Timecountutil extends Countdowntimer {
Private Activity mactivity;
Private button btn;//button

     //In this construction method you need to pass in three parameters, one is activity, One is the total time millisinfuture, the other is Countdowninterval, and then you do this on which button, just pass this button over to it.
< span> 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);//settings cannot be clicked
Span style= "White-space:pre" >
Btn.settext (millisuntilfinished/1000 + "seconds to Resend");//Set countdown time

The Set button is grayed out and is not clickable at this time
Btn.setbackground (Mactivity.getresources (). getdrawable (R.drawable.bg_duck_back));
Spannable span = new spannablestring (Btn.gettext (). toString ());//Gets the text of the button
Span.setspan (New Foregroundcolorspan (color.red), 0, 2, spannable.span_inclusive_exclusive);// The countdown time is shown in red.
Btn.settext (span);

}


@SuppressLint ("Newapi")
@Override
public void OnFinish () {
Btn.settext ("Re-obtain verification code");
Btn.setclickable (TRUE);//Regain click
Btn.setbackground (Mactivity.getresources (). getdrawable (R.drawable.bg_btn_back));//restore background color

}


}

Calling methods



Then in the method that needs to use this new object, and then call Start ();

countutil timecountutiltimecountutil = new Timecountutil (this, 60000, N, verficationbtn);
Timecountutil.start ();

Get Verification Code
Getverificationcode (Phonenum);

Implementation of


is not very simple, hope can help small friends!

Android get verification code after Countdown program

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.