Android Auto-fill SMS Verification code

Source: Internet
Author: User

Broadcast class

Package Com.examp.azuoyoutong.listner;

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

Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.telephony.SmsMessage;
Import Android.text.TextUtils;
Import Android.util.Log;

/**
* SMS-Broadcast
* */
public class Smsbroadcastreceiver extends Broadcastreceiver {
Private MessageListener Mmessagelistener;
public static final String sms_received_action = "Android.provider.Telephony.SMS_RECEIVED";
Private String Patterncoder = "(? <!\\d) \\d{6} (?! \\d) ";

Public Smsbroadcastreceiver () {
Super ();
}

@Override
public void OnReceive (context context, Intent Intent) {
TODO auto-generated Method Stub
if (Intent.getaction (). Equals (Sms_received_action)) {
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 ();
LOG.D ("logo", "message" + message);
Short-interest mobile phone number. +86 opening?
String from = Sms.getoriginatingaddress ();
LOG.D ("logo", "from" + from);

//
//Filter The sending number of text messages that do not need to be read
if ("+8613450214963". Equals (sender)) {
mmessagelistener.onreceived (content);
Abortbroadcast ();
// // }

if (! Textutils.isempty (from)) {
String code = PATTERNCODE (message);
if (! Textutils.isempty (code)) {
Mmessagelistener.onreceived (code);
}
}
}

}

}

/**
* 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;
}

Callback Interface
Public interface MessageListener {
public void onreceived (String message);
}

public void Setonreceivedmessagelistener (MessageListener messagelistener) {
This.mmessagelistener = MessageListener;
}

}

The activity registers the broadcast and displays the Verification code:

private void Initbroastcast () {
TODO auto-generated Method Stub
Intentfilter intentfilter= new Intentfilter ();
Intentfilter.addaction (smsbroadcastreceiver.sms_received_action);
Intentfilter.setpriority (Integer.max_value);
Msmsbroadcastreceiver = new Smsbroadcastreceiver ();
This.registerreceiver (Msmsbroadcastreceiver, Intentfilter);

Msmsbroadcastreceiver.setonreceivedmessagelistener (New Smsbroadcastreceiver.messagelistener () {
@Override
public void onreceived (String message) {

Reg_tel_code.settext (message);
Tt.showlongtoast (regactivity.this, message);
}
});
}

Note that the cancellation of the broadcast;


@Override
protected void OnDestroy () {
Super.ondestroy ();
Logout SMS Listening Broadcast
This.unregisterreceiver (Msmsbroadcastreceiver);
}

Also, be sure not to forget to read the SMS permissions in the XML configuration:

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

Android Auto-fill SMS 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.