Android development: Your SMS Verification Code (2), android Verification Code

Source: Internet
Author: User

Android development: Your SMS Verification Code (2), android Verification Code

A gentleman is eager to speak in words.-The Analects of Confucius

Recently, when we feel unwell, we have noticed that the body is really the most important thing. No matter how busy we are, we must rest on time, stick to running, exercise, and do some cervical exercises, don't let us get the money we earn to see a doctor. Let's stick to it together. Don't let the young man blind our eyes.

Your SMS verification code is developed on Android (1). We mainly talk about aggregated data and the preparation and configuration of developing SMS verification codes using aggregated data, this article introduces the implementation of complete functions and UI. If you have any mistakes, please correct them. If you have any questions, please leave a message. Thank you.

First, let's look at the first interface.


On this page, enter the mobile phone number and the destination of the text message. Click Next and a Dialog box is displayed.


Click "OK" to call the sendCaptcha method provided by aggregate data. We have used this method in the first article.

/* Click "OK" and call the sendCaptcha method to submit the mobile phone number code (return code) to the server: SERVER: 0 success, 1 error; Local:-2 local network exception, -3 server network exception,-4 Resolution error,-5 initialization exception. Reason: the reason for the success or error. Result (returned result): it is in JSON format and is null if an error occurs or no return value is returned. */SmsCaptcha. sendCaptcha (phone, new ResultCallBack () {@ Overridepublic void onResult (int code, String reason, String result) {closeDialog (); if (code = 0) {afterCapterRequested ();}}});
When the returned code = 0, it indicates that the SMS verification code is successfully sent. We need to jump to the interface, the code is as follows: 
// When code = 0 succeeds, jump to Activityprivate void afterCapterRequested () {String phone = etPhoneNumber. getText (). 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); // upload the mobile phone number and the formatted mobile phone number to intent. putExtra ("formatedPhone", fomatedPhone); intent. putExtra ("phone", phone); startActivity (intent);} // format the phone number. Format: + 86 131 4118 2951 private 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 following figure shows the page to jump to. On this page, you need to enter the text message verification code. After you enter the text message verification code and click Next, the commitCaptcha method of aggregated data is called to compare the data,
SmsCaptcha. commitCaptcha (phoneNumbber, verfiyCode, new ResultCallBack () {/* code: return code: SERVER: 0 success; 1 error; Local:-2 local network exception;-3 server network exception; -4 Resolution error;-5 initialization exception reason: the returned information is successful or the cause of the error. result: returned result in JSON format. 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 verification code 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 that the parameter required for this method is ① mobile phone number ② mobile phone verification code. After we call this method, the server that aggregates data will judge whether this verification code is the verification code sent to this mobile phone number just now, after verification, return the result to me. We can perform corresponding operations based on the returned result. In the above interface, we found a countdown function. Here I will talk about the ideas. Of course, there are many countdown methods. In this Demo, the countdown is placed in a textView of tvCountDown. First 
// R. string. the content of receiveMessgeCountDown is: <Data> it takes about <font color = #209526> % s </font> seconds to receive text messages </Data> // This sentence is to replace the % Number in html with time. drop String unReceive = getResources (). getString (R. string. receiveMessgeCountDown, time); // set the html-formatted string to the text box tvCountDown. setText (Html. fromHtml (unReceive ));
In this way, html formatted text can be filled into tvCountDown. 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 public void run () {while (time --> 0) {// the time after the minus 1 message is used to replace the "%" in <font color = #209526> % s </font> seconds </Data> before <Data> receives the SMS. countDownTime = CaptchaActivity. this. getResources (). 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. unreceiveMessage, time); upDateTvCountDown (countDownTime); time = RETRY_INTERVAL ;}}). start () ;}// update tvCountDownprivate void upDateTvCountDown (final String countDownTime) {runOnUiThread (new Runnable () {@ Override public void run () {tvCountDown. setText (Html. fromHtml (countDownTime); tvCountDown. setEnabled (false );}});}
Conclusion: The SMS verification code function of aggregate data is mainly used in two ways: sendCaptcha, and commitCaptcha. On this basis, we can change the interface as needed. The above describes the main logic of the SMS verification code.

Now, we have discussed the knowledge about using aggregated data for mobile phone verification. Please leave a message.
Complete text message Verification Code project source code, click here



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.