Android mobile phone number registration binding mobile phone number to obtain SMS verification code example

  • 2021-07-06 11:46:37
  • OfStack

This paper describes a common function-the function of registering or binding the mobile phone number to obtain the verification code in the mobile phone app, that is, the short message verification function

The specific effect is that you fill in the mobile phone number in the registration interface, click the Get Verification Code button, and then you will receive a verification message. After filling in the verification code, click the Registration button, and if the verification is correct, you can jump to another interface

1. First of all, everyone needs to register an account in mob official website. mob is a free SMS verification platform

2. Create an application in the background of mob. com

3. Download the corresponding sdk

4. Import sdk into your project as an library

5. You can now write code in your project to use this feature 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 activity (this is fixed and cannot be modified, just copy and paste it into your configuration file)


<activity  
android:name="cn.smssdk.SMSSDKUIShell"  
android:configChanges="keyboardHidden|orientation|screenSize"  
android:theme="@android:style/Theme.Translucent.NoTitleBar"  
android:windowSoftInputMode="stateHidden|adjustResize"/>  

Register 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="vertical" >  
  
  <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="10"  
    android:hint=" Please enter your 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"  
    android:layout_below="@+id/login_input_phone_et"  
    android:layout_marginTop="44dp"  
    android:hint=" Please enter the verification code "  
    android:inputType="textPassword" >  
  </EditText>  
  
  <Button  
    android:id="@+id/login_request_code_btn"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_alignRight="@+id/login_input_phone_et"  
    android:layout_alignTop="@+id/login_input_code_et"  
    android:text=" Get verification code " />  
  
  <Button  
    android:id="@+id/login_commit_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=" Registration " />  
  
</RelativeLayout>  

Registration 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 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();  
  }  
  
  /** 
   *  Initialize control  
   */  
  private void init() {  
    inputPhoneEt = (EditText) findViewById(R.id.login_input_phone_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 verification sdk  
    SMSSDK.initSDK(this, APPKEY, APPSECRETE);  
    EventHandler eventHandler = new EventHandler(){  
      @Override  
      public 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 listening 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.  Judge the mobile phone number by rules   
      if (!judgePhoneNums(phoneNums)) {  
        return;  
      } // 2.  Pass sdk Send SMS Verification   
      SMSSDK.getVerificationCode("86", phoneNums);  
  
      // 3.  Make the button unclickable and display the countdown (getting it)   
      requestCodeBtn.setClickable(false);  
      requestCodeBtn.setText(" Resend (" + 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:  
      // Submit the received verification code and mobile phone number for re-verification   
      SMSSDK.submitVerificationCode("86", phoneNums, inputCodeEt  
          .getText().toString());  
      //createProgressBar();  
      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);  
        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) {  
          //  After SMS registration is successful, return MainActivity, Then prompt   
          if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//  Submit verification code successfully   
            Toast.makeText(getApplicationContext(), " Submit verification code successfully ",  
                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 verification code ",  
                Toast.LENGTH_SHORT).show();  
          } else {  
            ((Throwable) data).printStackTrace();  
          }  
        }  
      }  
    }  
  };  
  
  
  /** 
   *  Judge whether the mobile phone number is reasonable  
   *  
   * @param phoneNums 
   */  
  private boolean judgePhoneNums(String phoneNums) {  
    if (isMatchLength(phoneNums, 11)  
        && isMobileNO(phoneNums)) {  
      return true;  
    }  
    Toast.makeText(this, " Incorrect input of mobile phone number! ",Toast.LENGTH_SHORT).show();  
    return false;  
  }  
  
  /** 
   *  Judge 1 Number of digits in a string  
   * @param str 
   * @param length 
   * @return 
   */  
  public static boolean isMatchLength(String str, int length) {  
    if (str.isEmpty()) {  
      return false;  
    } else {  
      return str.length() == length ? true : false;  
    }  
  }  
  
  /** 
   *  Verify the mobile phone format  
   */  
  public static boolean isMobileNO(String mobileNums) {  
    /* 
     *  Move: 134 , 135 , 136 , 137 , 138 , 139 , 150 , 151 , 157(TD) , 158 , 159 , 187 , 188 
     *  Unicom: 130 , 131 , 132 , 152 , 155 , 156 , 185 , 186  Telecommunications: 133 , 153 , 180 , 189 , ( 1349 Satcom)  
     *  To sum up, it is the first 1 Bit must be 1 , No. 2 Bit must be 3 Or 5 Or 8 The other location can be 0-9 
     */  
    String telRegex = "[1][358]\\d{9}";// "[1]" On behalf of 1 Bits are digits 1 , "[358]" On behalf of 2 Bits can be 3 , 5 , 8 In 1 A, "\\d{9}" After the representative, it can be 0 ~ 9 The number of, there are 9 Bit.   
    if (TextUtils.isEmpty(mobileNums))  
      return false;  
    else  
      return mobileNums.matches(telRegex);  
  }  
  
  /** 
   * progressbar 
   */  
  private void createProgressBar() {  
    FrameLayout layout = (FrameLayout) findViewById(android.R.id.content);  
    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(  
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_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();  
  }  
}  

I will not write the successful interface for verification. I hope everyone can learn to master the realization skills of Android to obtain SMS verification code through this example.


Related articles: