Android to achieve automatic filling to obtain verification code function _android

Source: Internet
Author: User

This article explains the Android automatic extraction of SMS Authentication code solutions, shared for everyone to refer to, the specific content as follows

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

Package Com.dongdongsongcan.app;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
 
Import Android.telephony.SmsMessage;
 
Import Com.su.dolphin.utils.LogUtil;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.regex.Matcher;
 
Import Java.util.regex.Pattern; public class Smsreceiver extends Broadcastreceiver {public interface Ismslistener {public void onsmsreceive (St
  Ring verifycode);
 
  private static Ismslistener Msmslistener;
  Public Smsreceiver (Ismslistener ismslistener) {msmslistener = Ismslistener;
 
  public static final String TAG = "Imichatsmsreceiver"; Android.provider.Telephony.Sms.Intents public static final String sms_received_action = "Android.provider.Telephony
 
 
  . sms_received "; @Override public void OnReceive (context context, Intent Intent) {LOGUTIL.D (">>>>>>>>& Gt;>>>>>>>>>>>>>&gT;>>>>>>>>>>>>>>>>>>>>>>>>>>>
    >>>>>>> "); 
 
      if (Intent.getaction (). Equals (Sms_received_action)) {smsmessage[] messages = getmessagesfromintent (intent);
for (Smsmessage message:messages) {//LOGUTIL.D (message.getoriginatingaddress () + ":" +//
Message.getdisplayoriginatingaddress () + ":" +////Message.getdisplaymessagebody () + ":" +
 
 
        Message.gettimestampmillis ());
        String msg = Message.getdisplaymessagebody ();
        LOGUTIL.D ("msg:" + msg);
        String verifycode = null;
        Pattern p = pattern.compile ("\\d{4}");
        Matcher m = p.matcher (msg);
          while (M.find ()) {Verifycode = M.group ();
        Break
        } logutil.d ("Verifycode" + Verifycode);
        if (Msmslistener!= null) {msmslistener.onsmsreceive (Verifycode);
 }     }} public final smsmessage[] Getmessagesfromintent (Intent Intent) {object[] messages =
 
    (object[]) Intent.getserializableextra ("PDUs");
 
 
    byte[][] Pduobjs = new byte[messages.length][];
 
    for (int i = 0; i < messages.length i++) {pduobjs[i] = (byte[)) messages[i];
 
    } byte[][] PDUs = new byte[pduobjs.length][];
 
    int pducount = Pdus.length;
 
    smsmessage[] msgs = new Smsmessage[pducount];
 
      for (int i = 0; i < Pducount i++) {pdus[i] = pduobjs[i];
 
    Msgs[i] = SMSMESSAGE.CREATEFROMPDU (Pdus[i]);
 
  return msgs;
 }
 
 
}

The above is the entire content of this article, I hope to help you learn.

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.