Project Summary mobile phone number + SMS Verification Code login, mobile phone number verification code
First, you need a phone number. Currently, many accounts set the account name to a mobile phone number, and then click the button to obtain the mobile phone verification code.
Second, you need the verification interface provided by the background for your mobile phone text message. Different companies use different interfaces. As a front-end, you don't need to consider it. You just need to ask the background to write interfaces for you, you just need to call it directly.
Activity_login.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "60dp" android: layout_marginTop = "20dp" android: layout_marginLeft = "10dp" android: layout_marginRight = "10dp" android: gravity = "center" android: orientation = "horizontal"> <EditText android: id = "@ + id/mobile_login" android: layout_width = "match_parent" android: layout_height = "40dp" android: layout_marginLeft = "10dp" android: hint = "Enter your mobile phone number" android: textSize = "16sp" android: background = "@ null" android: inputType = "number"/> </LinearLayout> <LinearLayout android: layout_width = "match_parent" android: layout_height = "50dp" android: layout_marginLeft = "10dp" android: layout_marginRight = "10dp" android: layout_marginTop = "15dp" android: gravity = "center" android: orientation = "horizontal"> <EditText android: id = "@ + id/yanzhengma" android: layout_width = "0dp" android: layout_height = "40dp" android: layout_weight = "4" android: background = "@ null" android: layout_marginLeft = "10dp" android: hint = "Enter the verification code" android: textSize = "16sp" android: inputType = "number"/> <Button android: id = "@ + id/getyanzhengma1" android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1.9" android: textSize = "13sp" android: gravity = "center" android: text = "get verification code" android: layout_gravity = "center"/> </LinearLayout> <Button android: id = "@ + id/login_btn" android: layout_width = "match_parent" android: layout_height = "40dp" android: layout_marginTop = "20dp" android: layout_marginLeft = "20dp" android: layout_marginRight = "20dp" android: text = "login" android: textSize = "16sp"/> </LinearLayout>
LoginActivity. java
Public class LoginActivity extends Activity implements View. onClickListener {private int countSeconds = 60; // countdown seconds private EditText mobile_login, yanzhengma; private Button success, login_btn; private Context mContext; private String usersuccess; private Handler mCountHandler = new Handler () {@ Override public void handleMessage (Message msg) {super. handleMessage (msg); if (countSeconds> 0) {-- countSeconds; getyanzhengma1.setText ("(" + countSeconds + ") to obtain the Verification Code"); mCountHandler. sendEmptyMessageDelayed (0, 1000);} else {countSeconds = 60; getyanzhengma1.setText ("please retrieve the verification code again") ;}}; private String userinfomsg; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); mContext = this; setContentView (R. layout. activity_login); initView (); initEvent (); initData ();} private void initView () {mobile_login = (EditText) findViewById (R. id. mobile_login); getyanzhengma1 = (Button) findViewById (R. id. getyanzhengma1); yanzhengma = (EditText) findViewById (R. id. yanzhengma); login_btn = (Button) findViewById (R. id. login_btn);} private void initEvent () {invoke (this); login_btn.setOnClickListener (this);} private void initData () {}@ Override public void onClick (View v) {switch (v. getId () {case R. id. getyanzhengma1: if (countSeconds = 60) {String mobile = mobile_login.getText (). toString (); Log. e ("tag", "mobile =" + mobile); getMobiile (mobile);} else {Toast. makeText (LoginActivity. this, "The Verification Code cannot be repeatedly sent", Toast. LENGTH_SHORT ). show ();} break; case R. id. login_btn: login (); break; default: break; }}// obtain information to log on to the public void login () {String mobile = mobile_login.getText (). toString (). trim (); String verifyCode = yanzhengma. getText (). toString (). trim (); RequestParams params = new RequestParams ("replace this with your login request interface"); x. http (). post (params, new Callback. progressCallback <String> () {@ Override public void onWaiting () {}@ Override public void onStarted () {}@ Override public void onLoading (long total, long current, boolean isDownloading) {}@ Override public void onSuccess (String result) {try {JSONObject jsonObject = new JSONObject (result); Log. e ("tag", "Login result =" + jsonObject); String success = jsonObject. optString ("success"); String data = jsonObject. optString ("data"); String msg = jsonObject. optString ("msg"); if ("true ". equals (success) {Log. e ("tag", "login data =" + data); JSONObject json = new JSONObject (data); token = json. optString ("token"); userId = json. optString ("userId"); // you can modify the getUserInfo ();} else {Toast. makeText (LoginActivity. this, msg, Toast. LENGTH_SHORT ). show () ;}} catch (JSONException e) {e. printStackTrace () ;}@ Override public void onError (Throwable ex, boolean isOnCallback) {}@ Override public void onCancelled (CancelledException cex) {}@ Override public void onFinished () {}}) ;}// obtain the verification code and determine whether the mobile phone number is private void getMobiile (String mobile) {if ("". equals (mobile) {Log. e ("tag", "mobile =" + mobile); new AlertDialog. builder (mContext ). setTitle ("prompt "). setMessage ("mobile phone number cannot be blank "). setCancelable (true ). show ();} else if (isMobileNO (mobile) = false) {new AlertDialog. builder (mContext ). setTitle ("prompt "). setMessage ("enter the correct mobile phone number "). setCancelable (true ). show ();} else {Log. e ("tag", "enter the correct mobile phone number"); requestVerifyCode (mobile) ;}// obtain the verification code information and request private void requestVerifyCode (String mobile) for the verification code) {RequestParams requestParams = new RequestParams. http (). post (requestParams, new Callback. progressCallback <String> () {@ Override public void onWaiting () {}@ Override public void onStarted () {}@ Override public void onLoading (long total, long current, boolean isDownloading) {}@ Override public void onSuccess (String result) {try {JSONObject jsonObject2 = new JSONObject (result); Log. e ("tag", "jsonObject2" + jsonObject2); String state = jsonObject2.getString ("success"); String verifyCode = jsonObject2.getString ("msg"); Log. e ("tag", "Get verification code =" + verifyCode); if ("true ". equals (state) {Toast. makeText (LoginActivity. this, verifyCode, Toast. LENGTH_SHORT ). show (); startCountBack (); // here is the request parameter} else {Toast. makeText (LoginActivity. this, verifyCode, Toast. LENGTH_SHORT ). show () ;}} catch (JSONException e) {e. printStackTrace () ;}@ Override public void onError (Throwable ex, boolean isOnCallback) {ex. printStackTrace () ;}@ Override public void onCancelled (CancelledException cex) {}@ Override public void onFinished (){}});} // use the regular expression to determine the telephone number public static boolean isMobileNO (String tel) {Pattern p = Pattern. compile ("^ (13 [0-9] | 15 ([0-3] | [5-9]) | 14 [, 7, 9] | 17, 7, 8] | 18 [0-9]) \ d {8} $ "); Matcher m = p. matcher (tel); System. out. println (m. matches () + "---"); return m. matches () ;}// obtain the verification code and perform the timing operation private void startCountBack () {(Activity) mContext ). runOnUiThread (new Runnable () {@ Override public void run () {getyanzhengma1.setText (countSeconds + ""); mCountHandler. sendEmptyMessage (0 );}});}}