Android how to automatically get SMS verification code via mobile phone _android

Source: Internet
Author: User

The example of this article for everyone to share the Android mobile phone automatically get the message authentication code function for everyone to refer to, the specific content as follows

1. SMS Listening and broadcasting

2, read the message content

3, intercept the message content "can be reg interception"

4, fill out to the corresponding control

Ps:devstore test did not get the verification code

SOURCE Fragment

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) {OBJ 
    Ect[] Objs = (object[]) Intent.getextras (). Get ("PDUs"); 
     for (Object Obj:objs) {byte[] PDU = (byte[]) obj; Smsmessage SMS = smsmessage.CREATEFROMPDU (PDU); 
     SMS content String message = Sms.getmessagebody (); 
     LOG.D ("logo", "message" + message); Short-term mobile phone number. 
     +86 opening? 
     String from = Sms.getoriginatingaddress (); 
     LOG.D ("logo", "from" + from); 
     Time is = new Time (); 
     Time.set (Sms.gettimestampmillis ()); 
     String time2 = time.format3339 (true); 
     LOG.D ("logo", from + "" + Message + "" + time2); 
     Strcontent = from + "" + message; 
     Handler.sendemptymessage (1); 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); /** * Matching 6 digits in the middle of the SMS (authentication code, etc.) * * @param patterncontent * @return/private String Patterncode (String pa Tterncontent) {if(Textutils.isempty (patterncontent)) 
  {return null; 
  Pattern p = pattern.compile (Patterncoder); 
  Matcher Matcher = P.matcher (patterncontent); 
  if (Matcher.find ()) {return matcher.group (); 
 return null; 
 }}</pre-->

At the end of the article for everyone to share two topics for your reference:

"PHP Verification Code Encyclopedia"

The ASP. NET Authentication Code Encyclopedia "

The above is Android through the mobile phone to automatically obtain the text message verification Code of the detailed codes, I hope for everyone to achieve automatic access to mobile phone message verification code help.

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.