Android SMS verification code
In the process of developing app, SMS verification is an essential operation when users register. Here we use a free third-party SMS to verify sdk-mop
First look at the renderings
Get appkey and appsecret
First, enter the official website and log in (if you don't have an account, register one yourself). Move the mouse to the right avatar and click to enter the background.
Official website home page
Select securitycodedk.
Select sms sdk
After entering the background page, click the Add button, fill in the application name (this can be written freely), select Android, and then click Add.
Add application
After adding successfully, you will enter the following interface, where you can see the successfully applied app key and app secret
Download SDK
Download SDK
Official documents
Here we choose the Android studio version
After downloading, unzip it. There is a smssdk folder with four packages. Put them under LIBS of our project
Then open the build gradle under the app and operate according to the following figure
This is the Android studio code
Configure androidmanifest xml
Add authority
Then click "application" to add the following activities:
Send SMS verification code using smssdk and verify
Core method
1. Initialize the SDK, which is a single instance and can be called multiple times; Any method must be initialized before it can be called
2. Register callback interface
3. Logoff callback interface
4. Class receiving callback
The initsdk method is the entrance to the sms sdk. You need to pass the app appkey and appsecret registered from the sharesdk application management background. If you fill in the error, subsequent operations will not be carried out. Registereventhandler is used to register an event receiver in smssdk. Smssdk allows developers to register any number of receivers. All receivers will receive messages when events are triggered.
Registereventhandler must be used with unregistereventhandler, otherwise memory leakage may occur.
5. Get the list of countries currently supported by SMS and return it in listening
6. Request to obtain SMS verification code and return it in listening
7. Submit SMS verification code and return it in monitoring
Getverificationcode is used to request the server to send a verification code. It needs to pass the country code and the mobile phone number receiving the verification code. The country code supporting this service is obtained in getsupportedcountries. The time interval for requesting getverificationcode should not be less than 60 seconds, otherwise the server will return the error of "too frequent operation". Submitvericationcode is used to submit the received SMS verification code to the server. After successful verification, the country code and telephone number will be returned through EventHandler.
Specific implementation steps
Initialize SDK.
//Fill in the appkey registered from the sms sdk application background
//Fill in the appsecret registered from the sms sdk application background
Declare EventHandler. (callbacks such as obtaining the verification code successfully and submitting the verification code successfully are implemented in EventHandler)
Note here: afterevent () is not in the main thread, so you cannot update the UI in afterevent () when the callback is completed. If you need to perform UI operations, please use handler.
Register SMS callback
This completes the initialization of SDK and the registration of SMS callback. Next, just call the interface for sending SMS and verification code
Destroy callback listening interface
Open the registration interface
This completes the simple SMS verification
PS:
Upload the project to GitHub with the address
GitHub project address
The above is the relevant knowledge of the code for Android to realize the SMS verification function introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!