Free text message verification for Android and free text message for android
Introduction
You are familiar with the text message verification function. It can be seen in many places, registration of new users or short-message verification payment and so on. The text message verification code is used to register members, greatly reducing illegal registration and greatly improving the security of user accounts.
Currently, many service providers that provide text message verification are available on the market, with charges and free of charge. If you are an individual developer, it is the most cost-effective to use it for free! Next I will introduce a free SMS verification platform ---Mob.com
The text message verification function provided by Mob allows you to quickly verify and match your address book friends, and provides 10000 free text message verification messages per app every day. Many developers want to integrate the text message verification function in their own apps. Let's enjoy the free "pleasure!
Application
1. Register as a mob platform user, go to the [Free SMS Verification Code SDK] in the control center, and click [add new application] on the interface to add your own application. After completing this step, you will be provided with an AppKey and an App Secret. You need to write down these two codes, which are of great use below.
2. Open the Web http://sms.mob.com/Download) to download the sdk. Decompress the package as follows:
SMSSDK is the SDK for text message Verification provided by the platform. The sample folder is a demo.
3. Import This sdk in ecliplse as a library and then introduce it into your own project as a library.
Next, you need to add the following permissions required by smssdk to AndroidManifest. xml:
<uses-permissionandroid:name="android.permission.READ_CONTACTS" /><uses-permissionandroid:name="android.permission.READ_PHONE_STATE" /><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><uses-permissionandroid:name="android.permission.INTERNET" /><uses-permissionandroid:name="android.permission.RECEIVE_SMS" /><uses-permissionandroid:name="android.permission.GET_TASKS" /><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" />
Add the following information under the activity Tag:
<activityandroid:name="cn.smssdk.SMSSDKUIShell"android:configChanges="keyboardHidden|orientation|screenSize"android:theme="@android:style/Theme.Translucent.NoTitleBar"android:windowSoftInputMode="stateHidden|adjustResize"/>
Next, call
SMSSDK.initSDK(this, “appkey”, “appsecret”);
The second and third parameters are the two codes provided by the application added on the mob platform!
Then add the registration callback listener Interface
SMSSDK.regeisterEventHandler(EventHandler);
EventHandler indicates that the function is destroyed, including four methods.
Public void onRegister (); // called when the callback object is registered
Public void beforeEvent (int event, Object data); // triggered before the operation
Public void afterEvent (int event, int result, Object data); // triggered at the end of the operation
Public void onUnregister (); // triggered during anti-registration
Generally, you only need to implement the afterEvent method. This method has three parameters. event indicates the operation type, result indicates the operation result, and data indicates the data returned by the operation. Commonly used events include EVENT_GET_VERIFICATION_CODE (obtain verification code) and EVENT_SUBMIT_VERIFICATION_CODE (submit verification code ).
After processing, you need to call the anti-registration function to destroy it.
SMSSDK.unresigterEventHandler(EventHandler);
Note: none of the four callback functions of EventHandler can be run in the UI thread. You must use handler to send messages to the UI thread for processing.
Let's look at my example below:
/*** Initialize the sms sdk */private void initSDK () {SMSSDK. initSDK (this, "xxxxxx", "xxxxxxxx"); EventHandler eventHandler = new EventHandler () {/*** triggered after the operation ** @ param event * parameter 1 * @ param result * parameter 2 SMSSDK. RESULT_COMPLETE indicates that the operation is successful, which is SMSSDK. * RESULT_ERROR indicates the operation failed * @ param data * event operation result */@ Overridepublic void afterEvent (int event, int result, Object data) {Message msg = new Message (); msg. arg1 = event; msg. arg2 = result; msg. obj = data; handler. sendMessage (msg) ;}}; // register the callback listener interface SMSSDK. registerEventHandler (eventHandler );}
@ Overridepublic void onClick (View v) {String phoneNums = phoneNumEt. getText (). toString (); switch (v. getId () {case R. id. back_iv: this. finish (); KeyBoardUtils. closeKeybord (phoneNumEt, RegisterActivity. this); break; case R. id. request_code_btn: // 1. determine the mobile phone number if (! JudgePhoneNums (phoneNums) {return;} // 2. use the sdk to send SMS verification. getVerificationCode ("86", phoneNums); // 3. turn the button into unclickable, and display the countdown (getting) requestCodeBtn. setClickable (false); requestCodeBtn. setText ("resend (" + I -- + ")"); new Thread (new Runnable () {@ Overridepublic void run () {for (int I = 30; i> 0; I --) {handler. sendEmptyMessage (-9); if (I <= 0) {break;} try {Thread. sleep (1000);} catch (InterruptedException e) {e. printStackTrace () ;}} handler. sendEmptyMessage (-8 );}}). start (); // 4. enable broadcast to receive and read text message break; case R. id. commit_btn: // judgePhoneNums (phoneNums); SMSSDK. submitVerificationCode ("86", phoneNums, inputCodeEt. getText (). toString (); createProgressBar (); // after the verification is passed, the smssdk registration code is deregistered. // SMSSDK. unregisterEventHandler (eventHandler); break; case R. id. clear_phone_iv: phoneNumEt. setText (""); break; case R. id. clear_code_iv: inputCodeEt. setText (""); break ;}}
Handler handler = new Handler () {public void handleMessage (Message msg) {if (msg. what =-9) {requestCodeBtn. setText ("resend (" + I -- + ")");} else if (msg. what =-8) {requestCodeBtn. setText ("Get Verification Code"); requestCodeBtn. setClickable (true);} else {int event = msg. arg1; int result = msg. arg2; Object data = msg. obj; Log. e ("event", "event =" + event); if (result = SMSSDK. RESULT_COMPLETE) {// if (event = SMSSDK. EVENT_SUBMIT_VERIFICATION_CODE) {// The verification code is submitted Toast. makeText (getApplicationContext (), "Verification Code submitted successfully", Toast. LENGTH_SHORT ). show (); Intent intent = new Intent (RegisterActivity. this, MainActivity. class); startActivity (intent);} else if (event = SMSSDK. EVENT_GET_VERIFICATION_CODE) {Toast. makeText (getApplicationContext (), "Verification code has been sent", Toast. LENGTH_SHORT ). show ();} else {(Throwable) data ). printStackTrace ();}}}}};
@Overrideprotected void onDestroy() {super.onDestroy();//this.unregisterReceiver(smsBroadcastReceiver);SMSSDK.unregisterAllEventHandler();}
So far, a text message verification function has been implemented. This platform has some restrictions. The app must be connected to the Internet, and the verification code can only be 4 characters. In addition, the verification text message cannot be customized. It can only be the [xxxx] xxxx verification code: 7521. In this form. If developers do not want these restrictions, I think they can only cooperate with operators. Download and upload the Demo later. Thank you for your advice!