This article has written a common function-the mobile phone app register or bind the handset number to obtain the authentication code function, namely the short message authentication function
The specific effect is that you fill in the Registration interface cell phone number, click to obtain the Verification Code button,---and then receive the verification message, fill in the Verification code after click on the registration button, if the correct verification can jump to another interface
1. First of all, we need to register an account in mob official website, Mob is a free SMS verification platform
2. Create application in mob.com background
3. Download the corresponding SDK
4. Import the SDK into your project as a library
5. You can now write code in your project to use this functionality provided by mob
The specific code is as follows:
First configure file Join 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 declare the activity (this is fixed and cannot be modified, copy and paste directly into your profile on the line)
<activity
android:name= "Cn.smssdk.SMSSDKUIShell"
android:configchanges= "keyboardhidden|orientation| ScreenSize "
android:theme=" @android: Style/theme.translucent.notitlebar "
android:windowsoftinputmode=" Statehidden|adjustresize "/>
Registering the interface XML layout file
<?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:orientation=" Vertica L "> <edittext android:id=" @+id/login_input_phone_et "android:layout_width=" 280DP "Android:
layout_height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true"
android:layout_margintop= "107DP" android:ems= "android:hint=" Please enter the mobile phone number "android:inputtype=" Phone "/>" <requestfocus/> <edittext android:id= "@+id/login_input_code_et" android:layout_width= "170DP" android:layout_height= "wrap_content" android:layout_alignleft= "@+id/login_input_phone_et" Androi d:layout_below= "@+id/login_input_phone_et" android:layout_margintop= "44DP" android:hint= "Please enter the CAPTCHA code" Android : inputtype= "Textpassword"; </EditText> <button android:id= "@+id/login_request_code_btn" android:layout_width= "Wrap_conten T "android:layout_height=" wrap_content "android:layout_alignright=" @+id/login_input_phone_et "Android:la yout_aligntop= "@+id/login_input_code_et" android:text= "Get Authentication Code"/> <button android:id= "@+id/login_c Ommit_btn "android:layout_width=" 280DP "android:layout_height=" wrap_content "android:layout_alignleft=" @
+id/login_input_code_et "android:layout_below=" @+id/login_input_code_et "android:layout_margintop=" 44DP "
android:text= "registered"/> </RelativeLayout>
Register Interface Activity
Package Com.mobdemo.view;
Import Cn.smssdk.EventHandler;
Import Cn.smssdk.SMSSDK;
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.Gravity;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.FrameLayout;
Import Android.widget.ProgressBar;
Import Android.widget.Toast;
Import Android.widget.FrameLayout.LayoutParams;
public class Loginactivity extends activity implements Onclicklistener {String Appkey = "101732155b605";
String appsecrete = "69d1850f4b74100266ab576b64e6cb16";
Mobile phone number input box private edittext inputphoneet;
Verification Code Input Box private EditText inputcodeet;
Get the Verification Code button Private button requestcodebtn; Register button Private button COMMITBTN
int i = 30;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_login);
Init (); }/** * Initialization control/private void init () {Inputphoneet = (edittext) Findviewbyid (r.id.login_input_ph
One_et);
Inputcodeet = (edittext) Findviewbyid (R.id.login_input_code_et);
REQUESTCODEBTN = (Button) Findviewbyid (R.ID.LOGIN_REQUEST_CODE_BTN);
COMMITBTN = (Button) Findviewbyid (R.ID.LOGIN_COMMIT_BTN);
Requestcodebtn.setonclicklistener (this);
Commitbtn.setonclicklistener (this);
Start SMS Verify SDK smssdk.initsdk (this, Appkey, appsecrete); EventHandler EventHandler = new EventHandler () {@Override public void afterevent (int event, int result, Ob
Ject data) {msg = new message ();
Msg.arg1 = event;
MSG.ARG2 = result;
Msg.obj = data; Handler.sendmeSsage (msg);
}
};
Register Callback Listener Interface Smssdk.registereventhandler (EventHandler);
@Override public void OnClick (View v) {String phonenums = Inputphoneet.gettext (). toString (); Switch (V.getid ()) {case R.ID.LOGIN_REQUEST_CODE_BTN://1. Pass the rule to judge the mobile number if (!judgephonenums phonenums
) {return; }//2.
Through the SDK to send SMS verification smssdk.getverificationcode ("", phonenums); 3.
Turn the button into an requestcodebtn.setclickable and show the Countdown (Getting) (false);
Requestcodebtn.settext ("Re-sending (" + i + ")");
New Thread (New Runnable () {@Override public void run () {for (; i > 0; i--) {
Handler.sendemptymessage (-9);
if (I <= 0) {break;
try {thread.sleep (1000);
catch (Interruptedexception e) {e.printstacktrace ();
}
} Handler.sendemptymessage (-8);
}). Start ();
Break
Case R.ID.LOGIN_COMMIT_BTN://will receive the verification code and mobile phone number submitted to check again Smssdk.submitverificationcode ("", Phonenums, Inputcodeet
. GetText (). toString ());
Createprogressbar ();
Break
}/** * * */Handler Handler = new Handler () {public void Handlemessage (msg) {
if (msg.what = = -9) {requestcodebtn.settext ("Resend" ("+ i +"));
else if (msg.what = = -8) {requestcodebtn.settext ("Get a Captcha");
Requestcodebtn.setclickable (TRUE);
i = 30;
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 successful, return mainactivity, then prompt if (event = = Smssdk. Event_submit_verification_code) {//Submit Verification code successfully toast.maketext (Getapplicationcontext (), "Submit Verification code successful", Toast.length_short. Show ();
Intent Intent = new Intent (loginactivity.this, Mainactivity.class);
StartActivity (Intent); else if (event = = Smssdk. Event_get_verification_code) {Toast.maketext (Getapplicationcontext (), "Getting Validation code", Toast.le
Ngth_short). Show ();
} else {(throwable) data). Printstacktrace ();
}
}
}
}
}; /** * To determine whether the mobile phone number is reasonable * * @param phonenums * * Private Boolean judgephonenums (String phonenums) {if (Ismatchlength (phonenums) && Ismobileno (phonenums))
{return true; Toast.maketext (This, "phone number entered incorrectly!")
", Toast.length_short). Show ();
return false; /** * To determine the number of digits of a string * @param str * @param length * @return/public static Boolean Ismatchl Ength (String str, int lengTH) {if (Str.isempty ()) {return false;
else {return str.length () = length? true:false; }/** * Verify mobile phone format/public static Boolean Ismobileno (String mobilenums) {* * * move: 134, 1 35, 136, 137, 138, 139, 150, 151, 157 (TD), 158, 159, 187, 188 * Unicom: 130, 131, 132, 152, 155, 156, 185, 186, Telecom: 133, 153, 180, 189, (1349) * summed up is the first must be 1, the second must be 3 or 5 or 8, other locations can be 0-9 * * * String Telregex = "[1][358]\\d{9}";/"[1]" represents the 1th digit 1
, "[358]" represents one of the second digits that can be 3, 5, 8, and "\\d{9}" represents a number that can be 0~9, with 9 digits.
if (Textutils.isempty (Mobilenums)) return false;
else return mobilenums.matches (Telregex); }/** * ProgressBar/private void Createprogressbar () {framelayout layout = (framelayout) find Viewbyid (Android.
R.id.content); Framelayout.layoutparams layoutparams = new Framelayout.layoutparams (layoutparams.wrap_content, LayoutParams.WR
Ap_content); Layoutparams.gravity = Gravity.centER;
ProgressBar Mprobar = new ProgressBar (this);
Mprobar.setlayoutparams (Layoutparams);
Mprobar.setvisibility (view.visible);
Layout.addview (Mprobar);
} @Override protected void OnDestroy () {Smssdk.unregisteralleventhandler ();
Super.ondestroy ();
}
}
Verify the successful interface I will not write, I hope you can learn from this example to grasp the Android access to SMS authentication code implementation skills.