Introduced
SMS Verification feature everyone is familiar with it. Can be seen in many places, registered new users or short-term verification payments. SMS verification using SMS verification code to register members, greatly reducing the illegal registration, to a large extent, improve the security of user accounts.
At present, there are many service providers that provide SMS verification, there are charges, there are free. If it's a personal developer, it's the best deal for free! Below I will introduce a free SMS verification platform---mob.com
The SMS verification feature provided by the Mob platform enables fast verification and matching of contacts friends, and provides 10,000 free SMS verification per app per day. many developers want to integrate SMS verification into their apps, so let's try the Free "thrill"!
Application
1. First you need to register as a mob platform user, then go to the "Free SMS Verification Code SDK" in the control center and click on "Add New app" On the interface to add your app. After this step is complete, you will be provided with a AppKey and an App Secret. These two codes need to be written down, and the following are of great use.
2. Open URL http://sms.mob.com/Download) to download the SDK. After decompression, such as:
SMSSDK is the Sdk,sample folder that the platform provides to verify the SMS is a demo.
3. In Ecliplse, import this SDK as a library and then introduce it as a repository into your own project.
Next, you need to add the following permissions in the Androidmanifest.xml SMSSDK:
<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 "/>
Under the Activity tab, add the following information:
<activityandroid:name= "Cn.smssdk.SMSSDKUIShell" android:configchanges= "keyboardhidden|orientation|screensize "Android:theme=" @android: Style/theme.translucent.notitlebar "android:windowsoftinputmode=" stateHidden| Adjustresize "/>
Next, when your project starts, call
SMSSDK.INITSDK (This, "Appkey", "Appsecret");
The second and third parameters are the two codes you add to your app on the mob platform!
Then add the register callback listener interface
Smssdk.regeistereventhandler (EventHandler);
where EventHandler represents a destroy function, including four methods.
public void Onregister ();//Called when the callback object is registered
public void Beforeevent (int event, Object data);//triggered before operation
public void Afterevent (int event, int result, Object data)//is triggered at the end of the operation
public void Onunregister ();//is triggered at the time of anti-registration
Generally only need to implement the Afterevent method is enough. The method has 3 parameters, the event represents the type of operation, result indicates the outcome of the operation, and data represents the return of the operation. The more commonly used EVENT has Event_get_verification_code (get verification Code), Event_submit_verification_code (submit verification code).
After processing is complete, you need to call the anti-registration to destroy it
Smssdk.unresigtereventhandler (EventHandler);
Note: None of the four callback functions of EventHandler can be run in the UI thread, and it is necessary to use handler to send a message to the UI thread.
Let's look at my example below:
/** * Initialize SMS SDK */private void Initsdk () {SMSSDK.INITSDK (this, "xxxxxx", "xxxxxxxx"); EventHandler EventHandler = new EventHandler () {/** * is triggered after operation * * @param event * Parameter 1 * @param result * Parameter 2 smssdk. Result_complete indicates that the operation was successful for SMSSDK. * Result_error indicates operation failed * result of @param data * Event operation */@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 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. Use the rule to determine if the phone number if (!judgephonenums (phonenums)) {return;}//2. Send SMS verification via SDK ("Smssdk.getverificationcode", phonenums);//3. Turn the button to non-clickable and display a countdown (getting) requestcodebtn.setclickable (false), Requestcodebtn.settext ("Resend (" + i--+ ")"), and new Thread ( New Runnable () {@Overridepublic void Run () {for (int i = -9; i > 0; i--) {handler.sendemptymessage (0); break;} try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ()}} Handler.sendemptymessage (-8);}}). Start ();//4. Open the broadcast to receive read SMS Break;case r.id.commit_btn://judgephonenums (phonenums); Smssdk.submitverificationcode ("Phonenums", Inputcodeet.gettext (). toString ()); Createprogressbar ();//after validation passes, Unregister the SMSSDK registration code//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) {//SMS registration is successful, return to Mainactivity, and then prompt for new friend if (event = = Smssdk. Event_submit_verification_code) {//Submit Verification Code Success Toast.maketext (Getapplicationcontext (), "Commit verification code succeeded", 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 (), "Authenticode sent", Toast.length_short). Show ();} else {((throwable) data). Printstacktrace ();}}}};
@Overrideprotected void OnDestroy () {Super.ondestroy ();//this.unregisterreceiver (Smsbroadcastreceiver); Smssdk.unregisteralleventhandler ();}
Summaryat this point, a text verification function has been implemented. This platform has some limitations, the app must be networked and the CAPTCHA can only be 4-bit. There is the verification text message can not be customized, can only be "XXXX" XXXX Verification code: 7521. In this form. If developers do not want to have these restrictions, I can only look for operators to cooperate. Downloadupload the demo later. Welcome everyone to advise!
Free SMS verification of Android from zero single row