Android SMS verification automatically get verification code

Source: Internet
Author: User

Android SMS verification automatically get verification code

Permissions:

<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>

Activity class

Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;

Import Android.annotation.SuppressLint;
Import android.app.Activity;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.telephony.SmsMessage;
Import Android.text.TextUtils;
Import Android.widget.EditText;

@SuppressLint ("Handlerleak")
public class Mainactivity extends Activity {
Private Broadcastreceiver Smsreceiver;
Private Intentfilter Filter2;
Private Handler Handler;
Private EditText et;
Private String strcontent;
Private String Patterncoder = "(? <!\\d) \\d{6} (?! \\d) ";

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
ET = (EditText) Findviewbyid (r.id.et);
Handler = new Handler () {
public void Handlemessage (Android.os.Message msg) {
Et.settext (strcontent);
};
};
Filter2 = new Intentfilter ();
Filter2.addaction ("Android.provider.Telephony.SMS_RECEIVED");
Filter2.setpriority (Integer.max_value);
Smsreceiver = new Broadcastreceiver () {
@Override
public void OnReceive (context context, Intent Intent) {
Object[] Objs = (object[]) Intent.getextras (). Get ("PDUs");
for (Object Obj:objs) {
Byte[] PDU = (byte[]) obj;
Smsmessage SMS = SMSMESSAGE.CREATEFROMPDU (PDU);
Content of SMS
String message = Sms.getmessagebody ();
Short-interest mobile phone number
String from = Sms.getoriginatingaddress ();
if (! Textutils.isempty (from)) {
String code = PATTERNCODE (message);
if (! Textutils.isempty (code)) {
strcontent = code;
Handler.sendemptymessage (1);
}
}
}
}
};
Registerreceiver (Smsreceiver, Filter2);
}

@Override
protected void OnDestroy () {
Super.ondestroy ();
Unregisterreceiver (Smsreceiver);
}

/**
* Match 6 digits in the middle of the SMS (Verification code, etc.)
*
* @param patterncontent
* @return
*/
private string Patterncode (string patterncontent) {
if (Textutils.isempty (patterncontent)) {
return null;
}
Pattern p = pattern.compile (Patterncoder);
Matcher Matcher = P.matcher (patterncontent);
if (Matcher.find ()) {
return Matcher.group ();
}
return null;
}
}

Android SMS verification automatically get verification code

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.