Android development belongs to your SMS Verification Code (II)

Source: Internet
Author: User

The gentleman wants to be in the speech and is sensitive to the line.-The Analects of Confucius

Recently, the body is a bit uncomfortable, only to notice the body is really the most important , no matter how busy we are, we must rest on time, insist on running, exercise, do some of the cervical spine and so on, do not let us earn money to see the doctor, we come together to insist Ah, do not let the young blinded our eyes.

In the Android development of your SMS Verification Code (a) we mainly talk about aggregated data, as well as the use of aggregated data development of SMS Verification code preparation and configuration, this article we will bring the full functionality of the implementation, as well as the interface UI implementation. if there is a false welcome to criticize, please leave a message if you have questions, thank you

First, let's take a look at the first interface.


We enter the phone number in this interface, and the destination of SMS, click Next will pop up a dialog


When you click OK, the call will call aggregated data to provide us with Sendcaptcha method, which we have used in the first article

     /* Click OK to call the Sendcaptcha method to submit mobile phone number              code (return code):               Server: 0 success, 1 error;  Local:-2 native network exception, -3 server network exception, 4 parsing error, 5 initialization exception.               Reason (return information): The cause of success or error. Result               (return result): Blank in JSON format, error, or no return value.             */Smscaptcha.sendcaptcha (phone,new resultcallback () {@Overridepublic void onresult (int code, String reason, String result) {Closedialog (); if (code==0) {aftercapterrequested ();}}   });
when the return of the code==0 that the SMS verification code sent successfully, we need to make the interface jump, the code is as follows
When Code=0 succeeds, jump to activityprivate void aftercapterrequested () {String phone=etphonenumber.gettext () to enter the verification code. ToString () . Trim (). ReplaceAll ("\\ss*", "" "); String Code=tvcountrynumber.gettext (). toString (). Trim (); String fomatedphone=code+ "" +splitphonenum (phone); Toast.maketext (This, "success", Toast.length_short). Show (); Intent intent=new Intent (); Intent.setclass ( Registactivity.this,captchaactivity.class);//The mobile phone number and formatted mobile phone number passed Intent.putextra ("Formatedphone", Fomatedphone); Intent.putextra ("Phone", phone); startactivity (intent);} Format phone number method, format after: +86 131 4118 2951private string Splitphonenum (String phone) {StringBuilder builder=new StringBuilder (phone); Builder.reverse (); for (int i=4,len=builder.length (); i<len;i+=5) {Builder.insert (I, ');} Builder.reverse (); return builder.tostring ();}
The layout of the jump interface is as followsin this interface we need to enter the SMS verification code, enter the SMS verification code, click Next when the aggregated data will be calledCommitcaptcha This method, than the method of the data,
Smscaptcha.commitcaptcha (phonenumbber,verfiycode,new resultcallback () {/*                     code: Return Code:                     Server: 0 successful; 1 error;                     Local:-2 local network exception; -3 server network exception;-4 parsing error;-5 initialization exception                     reason: Returns the message success or the cause of the error.                     Result: Returns the results, in JSON format. Empty when error or no return value. */@Overridepublic void onresult (int code,string reason,string result) {if (code==0) { LOG.I (tag,code+ ""); LOG.I (Tag,reason); LOG.I (Tag,result); Toast.maketext (Captchaactivity.this, "The captcha you entered is correct", Toast.length_short). Show (); CaptchaActivity.this.finish ();} Else{toast.maketext (Captchaactivity.this, "The verification code you entered is incorrect", Toast.length_short). Show ();}});

we can see this method required parameters are ① mobile phone number ② mobile phone verification Code , we call this method after the aggregation of data server will be judged, this verification code is just sent to this phone number of the verification code, verify that I returned the results, We can do it according to the result of the return. in the above interface we found that there is a countdown function, I am here to say the idea, of course, this countdown method has a lot. In this demo the countdown is placed in a tvcountdown of a textview, first
R.string.receivemessgecountdown content for:<data> to receive SMS about <font color= #209526 >%s</font> seconds </data >//This sentence is to use time to replace the% number in the HTML string unreceive=getresources (). getString (r.string. receivemessgecountdown,time);// Set the Html formatted string to the Countdown text box Tvcountdown.settext (html.fromhtml (unreceive));
This allows us to populate the Tvcountdown with HTML formatted text. The next step is the countdown method.
Private final int retry_interval =60;private int time=retry_interval;                 Countdown method private void Countdown () {New Thread (new Runnable () {@Override The time of the public void run () {while (time-->0) {//minus one) <Data> receive SMS takes about &LT;F Ont color= #209526 >%s</font> sec </Data>% to replace String countdowntime=captchaactivity.this. getres              Ources (). getString (r.string. receivemessgecountdown,time);              Update Tvcountdown Updatetvcountdown (Countdowntime) on the main thread;                  try {Thread. Sleep (1000);                     } catch (Interruptedexception e) {e.printstacktrace (); }} String countdowntime=captchaactivity.this. getresources (). getString (r.string. unrece               Ivemessage, time);               Updatetvcountdown (Countdowntime);           Time = Retry_interval; }}). StarT ();}                  Update tvcountdownprivate void Updatetvcountdown (final String countdowntime) {runonuithread (new Runnable () {) on the main thread @Override public void Run () {Tvcountdown.settext (Html. fromhtml (countdo                  Wntime));                Tvcountdown.setenabled (FALSE);     }           }); }
Summary: In fact, the use of aggregated data SMS verification code function, the main two methods, one is to obtain verification code method Sendcaptcha, one is to submit verification code to verify the method Commitcaptcha. Based on this, we can make changes to the interface as required. The above is the main logic of SMS verification code is introduced.

OK, about the use of aggregated data to achieve mobile phone verification knowledge point is over, Welcome to leave a message .
SMS Verification Code complete project source code, Poke here



Android development belongs to your SMS Verification Code (II)

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.