Android sends SMS verification code and automatically gets captcha fill text box

Source: Internet
Author: User

Android registration to send SMS verification code and automatically get text messages, intercept digital verification code fill text box.

I. Access to SMS Platform

First of all need to select the SMS platform access, where the use of the hazelnut cloud SMS platform (http://smsow.zhenzikj.com),

Two minutes to apply for the test account, giving 100 test messages.

Android uses Java's jar package to develop

Jar Download: http://smsow.zhenzikj.com/doc/sdk.html

API Document: Http://smsow.zhenzikj.com/doc/java_sdk_doc.html

Use test Account login Admin background get AppID, Appsecret, Address: Http://sms.zhenzikj.com/zhenzisms_user

Open in "My Apps", "Details":

1. Installation

The downloaded SDK contains only one jar file and does not rely on any other jar packages or files to be imported directly into the project for use.

2. Usage

Initialize zhenzismsclient with pre-applied AppID, Appsecret:

Zhenzismsclient client = new Zhenzismsclient (appId, Appsecret);
AppId and Appsecret are distributed by SMS platform.

1) Send SMS

String result = Client.send ("15811111111", "Your verification code is 4534, valid time is 5 minutes");

Send method for single text message

Parameter 1: Receiver mobile number, parameter 2: SMS Content

The returned result is a JSON-formatted string, code: Send status, 0 for success. Non-0 fails to send, can view error message from data

{"Code": 0, "Data": "Sent Successfully"}

Two. Get Verification code automatically

The general idea needs to be done in the following steps:
  1. Get SMS Content

  2. Determine if the text message contains a verification code, if there is a withdrawal, if not to inform the user does not match the SMS Verification Code

  3. Populate the text box with the matching SMS verification code

Start making

1.) Request SMS Permission

<uses-permission android:name= "Android.permission.RECEIVE_SMS"/> <!--receive SMS permission--><uses-permission Android:name= "Android.permission.READ_SMS"/> <!--read SMS rights--

Attention!!!
Since Google introduced runtime permissions in Android6.0, so long as the API version is greater than 23 requires permission to apply, here is a very simple and fast third-party library for the use of run-time permissions to request, Android 6.0 Runtime Permissions third-party library use- Rxpermissions

2.) Register SMS Recipients

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

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

SMS recipients are configured in the Androidmanifest.xml file:

<receiver android:name= ". Smsreceiver "> <intent-filter android:priority=" "> <action android:name=" android.provider.Te Lephony.sms_received "/> </intent-filter></receiver>

Note Set this broadcast receiver to the highest level (1000)

Smsreceiver.java

public class Smsreceiver extends Broadcastreceiver {private static final String TAG = "Smsreceiver";    @Override public void OnReceive (context context, Intent Intent) {//For the operation of obtaining SMS getmsg (context, Intent); }}



3.) Then get the SMS content

Private void getmsg (context context, intent intent)  {         //pdus Text message Unit pdu        //parsing SMS content          Object[] pdus =  (object[])  intent.getextras (). Get (" PDUs ");        assert pdus != null;         for  (Object pdu : pdus)  {             //Package SMS Parameters Object           &NBSP;&NBSP;&NBSP;SMSMESSAGE&NBSP;SMS&NBSP;=&NBSP;SMSMESSAGE.CREATEFROMPDU ((byte[]) &NBSP;PDU);             String number =  Sms.getoriginatingaddress ();             string  body = sms.getmessagebody ();             //write his own logic of handling              //Get SMS Verification Code              getcode (context, body);        }     }

Through the Smsmessage object can get the text message sent number, text message content and related information.

Match the verification code and copy it to the Clipboard

Here we put the verification into the clipboard, of course, you can also directly fill it into the text box

Private void getcode (context context, string body)  {         //get Clipboard Manager: &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CLIPBOARDMANAGER&NBSP;CM  =  (Clipboardmanager)  context.getsystemservice (context.clipboard_service);         pattern pattern1 = pattern.compile ("(\\d{6})");//extract six digits          matcher matcher1 = pattern1.matcher (body);//To match         pattern pattern2 = pattern.compile ("(\\d{4})") ;//extract four-bit digital         matcher matcher2 = pattern2.matcher ( body);//Match         if  (Matcher1.find ())  {//match Success              string code = matcher1.group (0);             //  creating a normal character type clipdata             clipdata mclipdata = clipdata.newplaintext ("Label",  code);             //  put Clipdata content in the system Clipboard.             cm.setprimaryclip (MClipData);             toast.maketext (context,  "Verification code copy succeeded",  toast.length_short). Show ();             log.d (tag,  "onreceive: "  + code);         } else  if  (Matcher2.find ())  {             String code = matcher2.group (0);             //  Create a normal character type CLIPDATA&NBSP;&NBsp;          clipdata mclipdata =  Clipdata.newplaintext ("Label",  code);             //  put Clipdata content in the system Clipboard.             cm.setprimaryclip (MClipData);             toast.maketext (context,  "Verification code copy succeeded",  toast.length_short). Show ();             log.d (tag,  "onreceive: "  + code);         } else  {            toast.maketext (context,  " No verification code detected ",  toast.length_short). Show ();            &NBSP;LOG.D (tag,  "onreceive: "  +  "No Verification Code detected");         }    } 

In this case, the verification code in the text message is matched by regular expression, because the verification code is mostly 4-bit or 6-bit, so the 4-bit verification code and 6-bit verification code are directly judged in order to simplify it.

OK, you are done.

Android sends SMS verification code and automatically gets captcha fill text box

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.