Android Combat Simple Tutorial-the 39th gun (third-party SMS verification Platform Mob and verification code automatically fill in the function combined with instances)

Source: Internet
Author: User

The user registers or retrieves the password commonly uses the short message authentication function, here we use the third party's SMS platform to verify the instance.

We use third-party SMS verification platform is mob, address: http://mob.com/

First, registered users, access to the SDK

Everyone can register themselves, get Appkey and Appsecret, and then download the SDK, the way the package is imported such as:



Second, the main code

Smssendforregisteractivity.java: (Get Verification code page)

Package Com.qiandaobao.activity;import Java.util.regex.matcher;import Java.util.regex.pattern;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.os.Handler;import Android.os.message;import Android.text.textutils;import Android.util.log;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.window;import Android.widget.button;import Android.widget.toast;import Cn.smssdk.eventhandler;import Cn.smssdk.smssdk;import Com.qiandaobao.broadcast.smsbroadcastreceiver;import Com.qiandaobao.broadcast.smsbroadcastreceiver.messagelistener;import Com.qiandaobao.view.DeletableEditText; Import Com.qiaodaobao.activity.r;public class Smssendforregisteractivity extends activity Implementsonclicklistener { Fill out the appkeyprivate static String APPKEY = "9aef0d828910" from the SMS SDK application background, and//fill in the Appsecretprivate static from the SMS SDK app background registration String Appsecret = "11b6db0240c87e2839a2995e05fcd7c5";p rivate deletableedittext Mphonedeletableedittext, MCodeDelEtableedittext;private Button Mgetcodebutton, mnextbutton;private String mphonestring, mcodestring;private Smsbroadcastreceiver msmsbroadcastreceiver; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); SetContentView ( r.layout.activity_sendsms); Initviews (); SMSSDK.INITSDK (This, APPKEY, Appsecret); EventHandler eh = new EventHandler () {@Overridepublic void afterevent (int event, int result, Object data) {Message msg = n EW Message (); msg.arg1 = Event;msg.arg2 = Result;msg.obj = Data;handler.sendmessage (msg);}}; Smssdk.registereventhandler (EH);} private void Initviews () {mphonedeletableedittext = (deletableedittext) Findviewbyid (r.id.et_phone_sendsms); Mcodedeletableedittext = (deletableedittext) Findviewbyid (r.id.et_code_sendsms); Mgetcodebutton = (Button) Findviewbyid (r.id.btn_getcode_sendsms); Mnextbutton = (Button) Findviewbyid (r.id.btn_next_sendsms); Mgetcodebutton.setonclicklistener (this); Mnextbutton.setoNclicklistener (this); msmsbroadcastreceiver = new Smsbroadcastreceiver (); Msmsbroadcastreceiver.setonreceivedmessagelistener (New MessageListener () {public void onreceived (String message) { Mcodedeletableedittext.settext (Getdynamicpassword (message));//intercept 6-bit verification code mcodedeletableedittext.setselection ( Getdynamicpassword (message). Length ());}); @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case R.id.btn_getcode_sendsms:if (! Textutils.isempty (Mphonedeletableedittext.gettext (). toString ())) {Smssdk.getverificationcode ("86", Mphonedeletableedittext.gettext (). ToString ()); mphonestring = Mphonedeletableedittext.gettext (). ToString ();} else {Toast.maketext (this, "Phone cannot be empty", 1). Show (); Break;case r.id.btn_next_sendsms:if (! Textutils.isempty (Mcodedeletableedittext.gettext (). toString ())) {Smssdk.submitverificationcode ("86", Mphonestring,mcodedeletableedittext.gettext (). toString ());} else {Toast.maketext (this, "Authenticode cannot be empty", 1). Show (); Break;default:break;}} Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {super.handlemessage (msg); int event = Msg.arg1;int result = Msg.arg2;object data = MSG.O bj 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 (smssendforregisteractivity.this,registeractivity.class); startactivity (Intent);} else if (event = = Smssdk. Event_get_verification_code) {Toast.maketext (Getapplicationcontext (), "Authenticode sent", Toast.length_short). Show ();}} else {(throwable) data). Printstacktrace (); Toast.maketext (smssendforregisteractivity.this, "Captcha error", Toast.length_short). Show ();}}; @Overrideprotected void OnDestroy () {Super.ondestroy (); Smssdk.unregisteralleventhandler ();} /** * Intercept a continuous 4-digit combination ([0-9]{"+ 4+"}) from a string to intercept a six-bit number before and after an assertion cannot be used to get a dynamic password from a text message * * @param str * SMS content * @return intercepted 4 Bit dynamic password */public String Getdynamicpassword (string str) {///6 is the number of bits of the captcha typically six-bit Pattern Continuousnumberpattern = pattern.compile (? <![ 0-9]) ([0-9]{"+ 4 +"}) (?! [0-9]); Matcher m = continuousnumberpattern.matcher (str); String Dynamicpassword = ""; while (M.find ()) {System.out.print (M.group ()));d Ynamicpassword = M.group ();} return Dynamicpassword;}}

The Smsbroadcastrecevier code used in the above code is in theAndroid Combat Simple Tutorial-the 36th gun (listen to SMS-implementation of SMS Verification code automatically fill in)Can be found in the ".

Third, layout files

Main.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:background=" @color/whit E "> <relativelayout android:id=" @+id/head_relat "android:layout_width=" Match_parent "Androi d:layout_height= "44DP" android:background= "@color/green" > <imagebutton android:id= "@+id/ba Ck_btn "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:            Layout_alignparentleft= "true" android:layout_centervertical= "true" android:layout_marginleft= "5DP" android:background= "@drawable/back"/> <textview style= "@style/title" Android : layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_centerinparent= "Tru E "Android:layout_centErvertical= "true" android:layout_marginleft= "5DP" android:layout_torightof= "@id/back_btn" Android:ellipsize= "End" android:gravity= "center" android:singleline= "true" android:text=            "Mobile verification"/> <imageview android:layout_width= "fill_parent" android:layout_height= "0.5DP"     Android:layout_alignparentbottom= "true" android:background= "#dddddd"/> </RelativeLayout> <com.qiandaobao.view.deletableedittext android:id= "@+id/et_phone_sendsms" android:layout_width= "match _parent "android:layout_height=" wrap_content "android:layout_below=" @+id/head_relat "android:layout_m Argin= "15DP" android:layout_margintop= "35DP" android:drawableleft= "@drawable/password_iv" Android:dra wableright= "@drawable/user_delete" android:gravity= "center_vertical" android:hint= "Please enter phone number"/> <com. Qiandaobao.view.DeletablEedittext android:id= "@+id/et_code_sendsms" android:layout_width= "200DP" android:layout_height= "Wrap_ Content "android:layout_alignparentleft=" true "android:layout_below=" @+id/et_phone_sendsms "android:l Ayout_margin= "15DP" android:layout_margintop= "35DP" android:drawableleft= "@drawable/password_iv" Android oid:drawableright= "@drawable/user_delete" android:gravity= "center_vertical" android:hint= "Verification Code"/> < Button android:id= "@+id/btn_getcode_sendsms" android:layout_width= "Wrap_content" Android:layout_heigh        t= "Wrap_content" android:layout_alignparentright= "true" android:layout_below= "@+id/et_phone_sendsms" android:layout_margintop= "15DP" android:layout_torightof= "@+id/et_code_sendsms" android:background= "@color/w Hite "android:paddingbottom=" 5DP "android:text=" Get the Captcha "android:textcolor=" @color/green "Androi D:textsize= "12SP"/>   <button android:id= "@+id/btn_next_sendsms" android:layout_width= "Match_parent" Android:layout_h        eight= "Wrap_content" android:layout_below= "@id/btn_getcode_sendsms" android:layout_centerhorizontal= "true" Android:layout_margin= "15DP" android:background= "@drawable/login_btn_bg" android:text= "Next" and Roid:textsize= "14SP"/></relativelayout>

Iv. permissions additions and configuration files

Open your project's "Androidmanifest.xml" and add the following 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.GET_TASKS"/><uses-permission android:name= " Android.permission.ACCESS_FINE_LOCATION "/>
then hit "Application" to add the following activity:

<activityandroid:name= "Com.mob.tools.MobUIShell" Android:theme= "@android: Style/theme.translucent.notitlebar" Android:configchanges= "Keyboardhidden|orientation|screensize" android:windowsoftinputmode= "stateHidden| Adjustresize ">
Five, running example


Then click to get the verification code, the text message will be automatically entered into the verification code edittext.

SMS Verification code can also be used to retrieve the password function, you can set the intent jump on their own can be.

Like a friend can pay attention to me! Thank you!



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Combat Simple Tutorial-the 39th gun (third-party SMS verification Platform Mob and verification code automatically fill in the function combined with instances)

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.