Android SMS Verification Code

Source: Internet
Author: User

The solution we use today is just one of the solutions for the integrated SMS Verification code feature of Android mobile devices.

Our approach is to use the SMS Verification SDK for aggregated data.


The interface of the program is as follows:



Implementation steps:

1. Apply for SMS Verification SDK data to the aggregation data Officer online, generate Appkey as shown in:



2. Configure the project and import the relevant SDK libraries into project:



3. Configure the Androidmanifest.xml file and add the following code to the application node:

        <meta-data            android:name= "Juhe_key"            android:value= "your Appkey"/>

4. Add the MyApplication class to SRC and set the Name property of application in the Androidmanifest.xml file to the MyApplication defined in SRC (for example, android:name= " Com.bear.smscaptcha.MyApplication "), calling interface initialization in the MyApplication OnCreate () method:

Import Com.thinkland.sdk.util.commonfun;import Android.app.application;public class MyApplication extends application {@Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate ();// Initializes the context information before using each component of the SDK, passing in applicationcontext//Note that the method is implemented/** * Initialization method before Setcontentview method * *  @param context * @ Needfriends If you need a buddy feature */commonfun.initialize (Getapplicationcontext (), false);}}

5. Complete the main demo class with the following contents:

Import Android.app.activity;import Android.content.pm.activityinfo;import Android.os.bundle;import Android.text.editable;import Android.text.html;import Android.text.textwatcher;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.textview;import Android.widget.toast;import Com.thinkland.sdk.sms.smscaptcha;import Com.thinkland.sdk.util.basedata.resultcallback;public class MainActivity  Extends Activity implements Onclicklistener{private Smscaptcha Mcaptcha;//SMS SDK main object private EditText etphonenum;  Mobile phone number input box private ImageView ivclear; Clear the icon for the content you have entered private Button btnnext; Next button private EditText etcaptchanum; Verification Code Input Box private Button btnsubmit; Submit Verification Code Private ImageView ivsmsclear; Private TextView tvunreceiveidentify;private static final int retry_interval = 60; Set a countdown time private int times = Retry_interval; @Overrideprotected void OnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate); setrequestedorientation (activityinfo.screen_orientation_portrait); Setcontentview (R.layout.activity_main); InitData (); Setupviews ();} private void InitData () {//Initialize SDK main Action Object Mcaptcha = Smscaptcha.getinstance ();} /** * Initialize interface control */private void Setupviews () {btnnext = (Button) Findviewbyid (r.id.btn_next); ivclear = (ImageView) Findviewby Id (r.id.iv_clear); Btnnext.setonclicklistener (this), Ivclear.setonclicklistener (this); etcaptchanum = (EditText) Findviewbyid (r.id.et_sms_captcha); btnsubmit = (Button) Findviewbyid (r.id.btn_submit); ivsmsclear = (ImageView) Findviewbyid (r.id.iv_sms_clear); Btnsubmit.setonclicklistener (this); Ivsmsclear.setonclicklistener (this); Tvunreceiveidentify = (TextView) Findviewbyid (r.id.tv_unreceive_identify); etphonenum = (EditText) Findviewbyid ( R.id.et_write_phone); Etphonenum.settext (""); Etphonenum.requestfocus (); Etphonenum.addtextchangedlistener (new Textwatcher () {@Overridepublic void ontextchanged (charsequence arg0, int arg1, int arg2, int arg3) {//TODO auto-generated method Stubrefreshviews (arg0);} @Overridepublic void beforetextchanged (charsequence arg0, int arg1, int arg2,int arg3) {//TODO auto-generated method stub } @Overridepublic void aftertextchanged (Editable arg0) {//TODO auto-generated Method stub}}); Determines whether the "clear all" button should appear according to the contents of EditText xprivate void Refreshviews (Charsequence s) {if (s.length () >0) { Btnnext.setenabled (True); ivclear.setvisibility (view.visible);} Else{btnnext.setenabled (false); ivclear.setvisibility (View.gone);}}  Countdown method private void Countdown () {New Thread (new Runnable () {public void run () {while (time--> 0) {final String unreceive = MainActivity.this.getResources (). getString (r.string.smssdk_receive_msg, time); Runonuithread (new Runnable () { public void Run () {Tvunreceiveidentify.settext (html.fromhtml (unreceive)); tvunreceiveidentify.setenabled (false);}}); try {thread.sleep;} catch (Exception e) {//Todo:handle exception}}time = Retry_interval;}}). Start ();} @Overridepublic void OnClick (view view) {SWITCH (View.getid ()) {Case r.id.btn_next://removes all the spaces in the string, string phone = Etphonenum.gettext (). toString (). Trim (). replace ("\ \ S* "," ");/** * Request SMS Verification Code * * Code return code: Server: 0 success; 1 error; Local:-2 local network exception; -3 server network exception;-4 parsing error;-5 initializing exception reason return information success or error reason. Result returns the results, in JSON format. Empty when error or no return value. */mcaptcha.sendcaptcha (Phone, new Resultcallback () {@Overridepublic void onresult (int code, string reason, string result {//TODO auto-generated method stubif (Code = = 0) {//Call succeeded}}); Countdown (); Break;case R.id.iv_clear:etphonenum.gettext ( ). Clear (); Break;case r.id.btn_submit:string PhoneNumber = Etphonenum.gettext (). toString (). Trim (). replace ("\\s*", "" ); String code = Etcaptchanum.gettext (). toString (). Trim (). replace ("\\s*", "" ");/** * Verify SMS Verification code * * Code return code: server: 0 successful; 1 error; Local:-2 local network exception; -3 server network exception;-4 parsing error;-5 initializing exception reason return information success or error reason. Result returns the results, in JSON format. Empty when error or no return value. */mcaptcha.commitcaptcha (PhoneNumber, Code, new Resultcallback () {@Overridepublic void onresult (int code, String reason , String result) {//TODO auto-generated method stubif (CODE = = 0) {//SMS Verification successful Toast.maketext (Mainactivity.this, "Verify success!!!!!!!! ", Toast.length_long). Show ();} else{//validation Failed Toast.maketext (Mainactivity.this, "failed!!!!!!!! "+ Reason, Toast.length_long). Show ();}}); Break;case r.id.iv_sms_clear:break;default:break;}}}


Finally, attach the demo code of the project source compression package:

Androidsmscaptcha

Android SMS Verification Code

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.