This article writes a common function - the function of obtaining verification codes for registering or binding mobile phone numbers in mobile apps, that is, the SMS verification function
The specific effect is that you fill in your mobile phone number in the registration interface, click the Get Verification Code button, --- and you will receive a verification text message. After filling in the verification code, click the registration button. If the verification is correct, you can jump to another interface.
1. First of all, everyone needs to register an account on the mob official website. mob is a free SMS verification platform
2. Create an application in the background
3. Download the corresponding SDK
4. Import SDK as a library into your project
5. You can now write code in your project using this function provided by mob
The specific code is as follows:
First, add permissions to the configuration file:
<uses-permission android:name=".READ_CONTACTS" /> <uses-permission android:name=".READ_PHONE_STATE" /> <uses-permission android:name=".WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name=".ACCESS_NETWORK_STATE" /> <uses-permission android:name=".ACCESS_WIFI_STATE"/> <uses-permission android:name="" /> <uses-permission android:name=".RECEIVE_SMS" /> <uses-permission android:name=".GET_TASKS" /> <uses-permission android:name=".ACCESS_FINE_LOCATION" />
Then declare the activity (this is fixed and cannot be modified, just copy and paste it into your configuration file)
<activity android:name="" android:configChanges="keyboardHidden|orientation|screenSize" android:theme="@android:style/" android:windowSoftInputMode="stateHidden|adjustResize"/>
Register interface xml layout file
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android: 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: 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 verification code" android:inputType="textPassword" > </EditText> <Button android: 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: 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="register" /> </RelativeLayout>
Registration interface activity
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; 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) { (savedInstanceState); setContentView(.activity_login); init(); } /** * Initialize the control */ private void init() { inputPhoneEt = (EditText) findViewById(.login_input_phone_et); inputCodeEt = (EditText) findViewById(.login_input_code_et); requestCodeBtn = (Button) findViewById(.login_request_code_btn); commitBtn = (Button) findViewById(.login_commit_btn); (this); (this); // Start SMS verification sdk (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; = data; (msg); } }; //Register callback listening interface (eventHandler); } @Override public void onClick(View v) { String phoneNums = ().toString(); switch (()) { case .login_request_code_btn: // 1. Determine the mobile phone number through the rules if (!judgePhoneNums(phoneNums)) { return; } // 2. Send SMS to verify through SDK ("86", phoneNums); // 3. Turn the button into non-clickable and display the countdown (geting) (false); ("Resend(" + i + ")"); new Thread(new Runnable() { @Override public void run() { for (; i > 0; i--) { (-9); if (i <= 0) { break; } try { (1000); } catch (InterruptedException e) { (); } } (-8); } }).start(); break; case .login_commit_btn: //Submit the received verification code and mobile phone number and check again ("86", phoneNums, inputCodeEt .getText().toString()); //createProgressBar(); break; } } /** * */ Handler handler = new Handler() { public void handleMessage(Message msg) { if ( == -9) { ("Resend(" + i + ")"); } else if ( == -8) { ("Get verification code"); (true); i = 30; } else { int event = msg.arg1; int result = msg.arg2; Object data = ; ("event", "event=" + event); if (result == SMSSDK.RESULT_COMPLETE) { // After the SMS registration is successful, return to MainActivity and prompt if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {// Submit the verification code successfully (getApplicationContext(), "Submit verification code successfully", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(, ); startActivity(intent); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { (getApplicationContext(), "Getting verification code", Toast.LENGTH_SHORT).show(); } else { ((Throwable) data).printStackTrace(); } } } } }; /** * Determine whether the mobile phone number is reasonable * * @param phoneNums */ private boolean judgePhoneNums(String phoneNums) { if (isMatchLength(phoneNums, 11) && isMobileNO(phoneNums)) { return true; } (this, "The mobile phone number is entered incorrectly!",Toast.LENGTH_SHORT).show(); return false; } /** * Determine the number of bits of a string * @param str * @param length * @return */ public static boolean isMatchLength(String str, int length) { if (()) { return false; } else { return () == length ? true : false; } } /** * Verify the phone format */ public static boolean isMobileNO(String mobileNums) { /* *Mobile: 134, 135, 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, (1349Sanitation) * In summary, the first position must be 1, the second position must be 3, 5 or 8, and the other positions can be 0-9 */ String telRegex = "[1][358]\\d{9}";// "[1]" means the first digit is the number 1, "[358]" means the second digit can be one of 3, 5, and 8, "\\d{9}" means that the number can be 0 to 9, and there are 9 digits. if ((mobileNums)) return false; else return (telRegex); } /** * progressbar */ private void createProgressBar() { FrameLayout layout = (FrameLayout) findViewById(); layoutParams = new ( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); = ; ProgressBar mProBar = new ProgressBar(this); (layoutParams); (); (mProBar); } @Override protected void onDestroy() { (); (); } }
I won’t write the interface for successful verification. I hope that through this example, you can learn and master the implementation skills of Android to obtain SMS verification codes.