Android registers and sends SMS verification codes, automatically obtains SMS messages, and intercepts the verification codes to fill the text box.


1. Access SMS platform

Firstly, we need to choose the SMS platform to access. Hazelcloud SMS platform (smsow.zhenzikj.com) is used here.

Two minutes to apply for a test account, 100 free test SMS.

Android can be developed using Java JAR packages


The jar download: smsow.zhenzikj.com/doc/sdk.htm…

The API documentation: http://smsow.zhenzikj.com/doc/java_sdk_doc.html


Use the test account login management background to obtain appId and appSecret, address: sms.zhenzikj.com/zhenzisms_u…




Open in “My Application “->” Details” :



1. Install

The downloaded SDK contains only one JAR file, and does not depend on any other JAR packages or files. It can be directly imported into the project and used.

2. Usage

Initialize ZhenziSmsClient with the pre-applied AppId and AppSecret:


ZhenziSmsClient client = new ZhenziSmsClient(appId, appSecret);Copy the code

AppId and AppSecret are assigned by SMS platforms


1) Send text messages


String result = client.send("15811111111"."Your verification code is 4534, valid for 5 minutes");Copy the code

The send method is used to send a single short message

Parameter 1: mobile phone number of the receiver. Parameter 2: SMS content

The result is a string in JSON format, with code: sent status and 0 indicating success. If the value is not 0, the sending fails. You can view error information in data

			{
			    "code": 0."data":"Sent successfully"
			}			Copy the code


Two. Automatically obtain the verification code

General train of thought

The following steps need to be done:

  1. Obtaining SMS Content

  2. Check whether the SMS contains a verification code. If so, extract the verification code. If not, inform the user that no verification code is matched

  3. Fill the matched SMS verification code in the text box


Started making

1.) Apply for the SMS permission

<uses-permission android:name="android.permission.RECEIVE_SMS"/ > <! --> <uses-permission android:name="android.permission.READ_SMS"/ > <! -- Read SMS permission -->Copy the code


Attention!!


Since Google introduced runtime permissions in Android6.0, it is necessary to apply for permissions as long as the API version is larger than 23. Here, it is recommended to use a very simple and fast third-party library to apply for runtime permissions.
Android 6.0 runtime permissions use of third-party libraries — RxPermissions


2.) Register SMS recipients

We know that every time the system receives a text message, it sends out an announcement,

To do this, the first thing we need to do is configure a broadcast receiver to respond to the broadcast

Androidmanifest.xml file to configure SMS recipient:

<receiver android:name=".SMSReceiver">
      <intent-filter android:priority="1000">
           <action android:name="android.provider.Telephony.SMS_RECEIVED" />
      </intent-filter>
</receiver>Copy the code


Note that set the level of this broadcast receiver to the highest (1000)

SMSReceiver.java

public class SMSReceiver extends BroadcastReceiver {

    private static final String TAG = "SMSReceiver"; @override public void onReceive(Context Context, Intent Intent) {public void onReceive(Context Context, Intent Intent); }}Copy the code




3.)

Next, the content of SMS is obtained

private void getMsg(Context context, Intent [] pdus = (Object[]) intent.getextras ().get() intent.getextras ()"pdus"); assert pdus ! = null;for(Object PDU: pdus) {// Encapsulate the Object of the SMS parameter SmsMessage SMS = smsmessage.createFrompdu ((byte[]) PDU); String number = sms.getOriginatingAddress(); String body = sms.getMessageBody(); // Get the SMS verification code getCode(context, body); }}Copy the code


The SmsMessage object displays the SMS number, content, and related information.

Match the verification code and copy it to the clipboard

Here we put the verification code on the clipboard, or you can fill it directly into the text field

Private void getCode(Context Context, String body) {// Get the clipboard manager:  ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); Pattern pattern1 = Pattern.compile("(\\d{6})"); // Extract the six digits Matcher matcher1 = pattern1.matcher(body); Pattern pattern2 = pattern.compile ("(\\d{4})"); // Extract four-digit Matcher matcher2 = pattern2.matcher(body); // Matchif(matcher1.find()) {String code = matcher1.group(0); MClipData = ClipData. NewPlainText ("Label", code); // Put the ClipData contents into the system clipboard. cm.setPrimaryClip(mClipData); Toast.makeText(context,"Verification code copied successfully", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onReceive: " + code);
        } else if(matcher2.find()) { String code = matcher2.group(0); MClipData = ClipData. NewPlainText ("Label", code); // Put the ClipData contents into the system clipboard. cm.setPrimaryClip(mClipData); Toast.makeText(context,"Verification code copied successfully", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onReceive: " + code);
        } else {
            Toast.makeText(context, "No captcha detected", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onReceive: " + "No captcha detected"); }}Copy the code

In this section, regular expressions are used to match the verification codes in SMS messages. Since most verification codes are of four or six digits, the verification codes of four and six digits are directly judged for simplicity


Ok, we’re done.