Copyright NOTICE: This article is Bo Master original article, without the permission of Bo Master do not casually reproduced.
First of all, many third-party SMS verification code can be, why should I choose Mob?
Because the mob SMS verification code is completely free, finish ~ ~ Free ~ Fee ~, so the boss let me use it
In fact, the personal feeling of mob SMS verification occurs very unstable time, will be delayed, and not approved before a mobile phone number only allow 5 times a day, a mob account can be sent up to 20 times a day.
The use of efficiency may be relatively low, but, it is finished ~ ~ Free ~ ~ ~ ~
Request Flow:
1. Request a server connected to smssdk with the user-supplied mobile number as a parameter
2. SMSSDK Server Check number format successfully, request SMS service provider to send SMS verification code to the mobile phone number
3. The phone terminal receives the verification code, the user enters the verification code
4. Verification code in the Application backend service side, the service side through the smssdk interface authentication, and then return the verification results to the mobile phone side
function Implementation process:
There are two types, the first is no GUI, the second is a GUI
There is a GUI refers to,smssdk Inside there is an interface, call this interface can use smssdk has been written for our login registration layout, SMS Verification code and SMS verification callback method;
Call the first GUI -free method when you do not want to use the layout provided in SMSSDK or just need SMS verification code .
One, no GUI integrated SMS Verification Code SMSSDK
1. Download the SMS Verification Code SDKhttp://www.mob.com/#/downloadDetail/SMS/android from Mob official website
2. Put the jar package under Android smssdk v2.0.1.zip into the libs Directory of the project, and Raw files, including so files in raw files , are pulled into the project's res . If the file cannot be found, you can modify The format of the SMSSDK-2.0.1 file to obtain it from RAR.
3. Configure the required permissions for the androidmanifest.xml :
<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.GET_TASKS"/>
<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/>
To add activityunder <application>:
<activity
Android:name= "Com.mob.tools.MobUIShell"
Android:theme= "@android: Style/theme.translucent.notitlebar"
Android:configchanges= "Keyboardhidden|orientation|screensize"
Android:windowsoftinputmode= "Statehidden|adjustresize"/>
4. Write code in activity , execute in 2 steps, one load, two send
Appkey and appsecret in applications for mob Applications
initialization of the SDK, Singleton, can be called multiple times, before any method call, you must first initialize the
SMSSDK.INITSDK (This, " your appkey", " your appsecret");
the first one of the parameters represents the country code, the Chinese
Smssdk.getverificationcode ("86", "mobile number");
This completes a simple request SMS verification code function.
The server verifies that the SMS verification code must first turn on the server authentication switch, otherwise it cannot be verified.
Second, there is GUI Interface Integration Call
1. GUI integration is roughly the same as without GUI , need to add a More in the Libs directory Smssdkgui-2.0.1.jar Bag
2. Integrated permissions configuration (same as no GUI interface)
3. Add code
Initialize
SMSSDK.INITSDK (This, " your appkey", " your appsecret");
calling code to open the GUI page
Open the registration page
Registerpage registerpage = new Registerpage ();
Registerpage.setregistercallback (New EventHandler () {
public void Afterevent (int event, int result, Object data) {
Resolve 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 (context);
static void |
Getsupportedcountries () Get a list of countries that SMS currently supports, and return in listening |
static void |
Getverificationcode (String country, String phone) Request for SMS Verification code, return in listening |
static void |
Submitverificationcode (String country, string phone, string code) Submit SMS Verification code, return in listening |
SMS SDK does not support SMS verification in all countries, and calls the Getsupportedcountries method to obtain a list of countries and country codes that the SDK can support.
The time interval for request Getverificationcode should not be less than a few seconds, otherwise the server will return an "operation too frequently" error.
The Submitverificationcode is used to submit a received SMS verification code to the server, which will return the Country Code and phone number via EventHandler after successful verification.
EventHandler is the SMS callback interface, must register before use, similarly, has the registration to have the logoff.
Smssdk.registereventhandler (EventHandler handler) Register callback interface
Smssdk.unregistereventhandler (EventHandler handler) Logoff callback interface
Registereventhandler must be used with Unregistereventhandler , otherwise it may cause a memory leak.
SMS Callback Code
EventHandler eh=new EventHandler () {
@Override
public void Afterevent (int event, int result, Object data) {
if (result = = Smssdk. Result_complete) {
Callback Complete
if (event = = Smssdk. Event_submit_verification_code) {
Submit Verification Code Success
}else if (event = = Smssdk. Event_get_verification_code) {
Get Verification Code Success
}else if (event ==smssdk. Event_get_supported_countries) {
returns a list of countries that support sending verification codes
}
}else{
((throwable) data). Printstacktrace ();
}
}
};
Smssdk.registereventhandler (EH); Register SMS Callback
Flowchart:
Introducing the server-side authentication interface take the following process
1. mobile phone Request SMS
2.SMSSDK Server Request SMS service provider
3. Text messaging service provider issued SMS
4. Send SMS Verification code to the developer server for verification
5. the developer server is sent to the smssdk server for verification
6.SMSSDK server back to developer server verification results
7. the developer server returns the verification result to the mobile phone
When there is no SMS Server authentication interface, my servers need to know if the user has verified the success.
1. mobile Phone Request SMS
2. SMSSDK Server Request SMS service provider
3. Text messaging service provider issued SMS
4. Send SMS Verification code to SMSSDK server for verification
5. SMSSDK server back to the phone verification results
6. Return the verification results to the developer server
The above is a personal summary, questions or inappropriate, please point out
The first time to write a blog, very excited ~ ~ ~
Then we'll work hard and hold on
The use of the mob SMS Verification Code SMSSDK