SMS verification code for Android

Source: Internet
Author: User

SMS verification code for Android

The solution we use today is only one of the solutions for android mobile devices to integrate the SMS verification code function.

We use the SMS verification sdk for aggregated data.

 

The program interface is as follows:

 

Steps:

1. Apply for SMS verification SDK data on the aggregated data official website and generate an AppKey, as shown in:

 

2. Configure the Project and import the relevant sdk Library to the Project ,:

 

3. Configure the AndroidManifest. xml file and add the following code to the Application node:

 

        
 

 

4. add the MyApplication class to src and add it to AndroidManifest. in the xml file, set the name attribute of the application to the MyApplication defined in src (for example, android: name = "com. bear. smscaptcha. myApplication "), call the interface initialization in the onCreate () method of MyApplication:

 

Import com. thinkland. sdk. util. commonFun; import android. app. application; public class MyApplication extends Application {@ Overridepublic void onCreate () {// TODO Auto-generated method stubsuper. onCreate (); // initialize context information before using SDK components, input ApplicationContext // note that this method must be implemented before the setContentView method./*** initialization method ** @ param context * @ needFriends requires friend function */CommonFun. initialize (getApplicationContext (), false );}}

5. Complete the main Demo class with the following content:

 

 

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; // main object of the SMS sdk private EditText etPhoneNum; // private ImageView ivClear in the mobile phone number input box; // clear the private Button btnNext icon with the entered content; // click private EditText etCaptchaNum in the next step; // The private Button btnSubmit in the verification code input box; // submit the verification code private ImageView ivSm SClear; private TextView tvUnreceiveIdentify; private static final int RETRY_INTERVAL = 60; // set a countdown time private int time = 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 the main sdk operation object m Captcha = SMSCaptcha. getInstance ();}/*** initialization interface control */private void setupViews () {btnNext = (Button) findViewById (R. id. btn_next); ivClear = (ImageView) findViewById (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_sm S_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 }});} // determine whether the "Clear all" button should appear Based on edittext content. xprivate void refreshViews (CharSequence s) {if (s. length ()> 0) {btnNext. setEnabled (true); ivClear. setV Isibility (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); tvUnrec EiveIdentify. setEnabled (false) ;}}); try {Thread. sleep (1000);} catch (Exception e) {// TODO: handle exception }}time = RETRY_INTERVAL ;}}). start () ;}@ Overridepublic void onClick (View view) {switch (view. getId () {case R. id. btn_next: // delete all 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 Resolution error;-5 initialization exception re The message returned by ason is successful or the cause of the error. result returned, in JSON format. 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 successful }}); 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 *", "");/*** verification SMS Verification code ** return code: SERVER: 0 success; 1 error; Local: -2 local network exception;-3 server network exception;-4 Resolution error;-5 initialization exception reason returned message success or error cause. result returned, in JSON format. 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) {// text message Toast. makeText (MainActivity. this, "Verification Successful !!!!!!!! ", Toast. LENGTH_LONG). show () ;}else {// Verification Failed Toast. makeText (MainActivity. this," failed !!!!!!!! "+ Reason, Toast. LENGTH_LONG). show () ;}}); break; case R. id. iv_sms_clear: break; default: break ;}}}


 

Finally, the project source code compressed package with the demo code is attached:

AndroidSMSCaptcha

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.