The code _android of Android to realize the function of SMS verification

Source: Internet
Author: User
Tags time interval

In our current development app process, when the user registers, the SMS authentication is the essential operation, here we are uses a free third party text message authentication Sdk-mop

First look at the effect chart


Get Appkey and Appsecret

First go to the official website, login (no account of their own to register a). Mouse to move to the right avatar, click into the background.


Website Home

Select SECURITYCODESDK.


Select SMS SDK

After entering the background page, click Add button, fill in the application name (this can be casually written), choose Android, click Add.


Add Application

Once you've added success, you'll get to the following interface, where you can see the app key and app Secret that just applied for success

Download SDK

Download SDK

Official documents

Here we choose the Androidstudio version

After the download, there is a SMSSDK folder with 4 packages, put them under the Libs of our project


Then open the Buildgradle under the app and follow the diagram below


This is Androidstudio code.

repositories{flatdir{dirs ' libs '}}dependencies {compile filetree (dir: ' Libs ', include: [' *.jar ']) testcompile ' junit:j unit:4.12 ' Compile name: ' SMSSDK-2.1.1 ', ext: ' AAR ' compile name: ' SMSSDKGUI-2.1.1 ', ext: ' AAR ' compile ' com.android.support:appcompat-v7:23.3.0 '}

Configure Androidmanifest.xml

Plus permissions

 <uses-permission android:name= "Android.permission.READ_CONTACTS"/> <uses-permission android:name= " Android.permission.READ_PHONE_STATE "/> <uses-permission android:name=" android.permission.WRITE_EXTERNAL_ STORAGE "/> <uses-permission android:name=" Android.permission.ACCESS_NETWORK_STATE "/> <uses-permission Android:name= "Android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name= " Android.permission.INTERNET "/> <uses-permission android:name= Android.permission.RECEIVE_SMS"/> < Uses-permission android:name= "Android.permission.READ_SMS"/> <uses-permission android:name= " Android.permission.GET_TASKS "/> <uses-permission android:name=" Android.permission.ACCESS_FINE_LOCATION " >

Then hit "Application" to add the following activity:

<activity  android:name= "Com.mob.tools.MobUIShell"  android:theme= "@android: style/ Theme.Translucent.NoTitleBar "  android:configchanges=" Keyboardhidden|orientation|screensize "  android: Windowsoftinputmode= "Statehidden|adjustresize"/>

Use SMSSDK to send SMS verification code and verify

Core approach

1. Initialize SDK, single case, can be called multiple times; before any method call, it must be initialized first.

INITSDK (context context, string Appkey, String appsecrect)

2. Register Callback Interface

Registereventhandler (EventHandler handler)

3. Logout Callback Interface

Unregistereventhandler (EventHandler handler)

4. Classes that receive callbacks

EventHandler

Initsdk method is the entrance to the SMS SDK, you need to pass the application Appkey and Appsecrete registered from the SHARESDK application management background, if you fill in the error, the subsequent operation will not be carried out. Registereventhandler is used to register an event receiver in Smssdk, SMSSDK allows the developer to register any number of receivers, and all receivers receive messages when the event is triggered.

Registereventhandler must be used in support of the Unregistereventhandler, or it may cause memory leaks.

5. Get the list of countries currently supported by SMS, return in listening

Getsupportedcountries ()

6. The request obtains the short message authentication code, returns in the monitor

Getverificationcode (String country, String phone)

7. Submit SMS Verification code, return in the Listening

Submitverificationcode (String country, string phone, string code)

Getverificationcode is used to request a service to send authentication code to the server, it needs to pass the state code and receive the authentication code of the mobile number, support the service of the country codes in the getsupportedcountries to obtain. The time interval for requesting getverificationcode should not be less than 60 seconds, otherwise the service end will return "too frequent" error submitverificationcode used to submit the received SMS authentication code to the server, After successful validation, the country code and phone number are returned via EventHandler.

Concrete implementation Steps

Initializes the SDK.

Fill in the Appkey from the SMS SDK application background Registration

 private static String Appkey = "154E3E410ABC3";

Fill in the Appsecret from the SMS SDK application background Registration

 private static String Appsecret = "05713446f9786e2ad096c46fd4735dfe"; SMSSDK.INITSDK (context, Appkey, Appsecret);

Declaration EventHandler. (all callbacks are implemented in EventHandler, such as the success of the authentication code and the successful submission of the verification code)

 EventHandler eh=new EventHandler () {  @Override public  void afterevent (int event, int result, Object data) {  I F (Result = = Smssdk. Result_complete) {   //callback Complete   if (event = = Smssdk. Event_submit_verification_code) {   ///Submit Verification code successfully   LOG.I ("EventHandler", "Submit validation code successful");   } else if (event = = Smssdk. Event_get_verification_code) {   //get Validation code success   LOG.I ("EventHandler", "Get validation code successful");   } else if (event ==smssdk. event_get_supported_countries) {   //Returns a list of countries that support sending authentication codes   LOG.I ("EventHandler", "return list of countries that support sending authentication codes");   }  else{(           (throwable) data). Printstacktrace ();   LOG.I ("EventHandler", "Callback Failed");  }  };

Note here: Afterevent () is not in the main thread, so the callback does not perform the update UI in Afterevent (), and if you need to perform a UI action, use handler.

Register SMS Callback

Smssdk.registereventhandler (EH); Register SMS Callback

This completes the SDK initialization and SMS callback registration, and then just call send SMS and verify the authentication code interface on the line

Destroying the callback listener interface

 Smssdk.unregisteralleventhandler ();

Open the Registration interface

 Open the registration page  registerpage registerpage = new Registerpage ();  Registerpage.setregistercallback (New EventHandler () {public   void afterevent (int event, int result, Object data) { c7/>//resolves the registration results   if (result = = Smssdk. Result_complete) {    @SuppressWarnings ("unchecked")    hashmap<string,object> Phonemap = (hashmap< String, object>) data;    String country = (string) phonemap.get ("Country");    String phone = (string) phonemap.get ("Phone");    Submit User Information    RegisterUser (country, phone);   }   }  );  Registerpage.show (this);

This completes the simple text message verification

Ps:

Upload the item to the GitHub, with the address attached.

GitHub Project Address

The above is a small set of Android to introduce the implementation of the text message verification of the code of the relevant knowledge, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.