Android Automatic backfill SMS verification code via broadcast

Source: Internet
Author: User

Every time we sign up for a software account, we usually need to enter it.

Verification code, and then the verification code needs to be received through the text message can be seen

So here's the problem. SMS received, we can directly fill in the Verification code

The input box that needs to be filled in.

Next time the code is posted

Declarations section

<textarea readonly="" name="code" class="java">Private Broadcastreceiver smsreceiver;//defines a broadcast receiver private intentfilter filter2;//private EditText Editcaptcha; Text message automatically fills in box private String strcontent; CAPTCHA content Private String Patterncoder = "(? <!\\d) \\d{4,10} (?! \\d) ";//verify SMS Content</textarea>


Code implementation part

Private Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) {Editcaptcha.settext (str (Content);};}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Editcaptcha = (EditText) Findviewbyid (r.id.login_editcaptcha); filter2 = new Intentfilter (); /Create Intent Filter filter2.addaction ("Android.provider.Telephony.SMS_RECEIVED");//Create Intent Action filter2.setpriority (integer.max_ VALUE)///set level max Smsreceiver = new Broadcastreceiver () {@Overridepublic void OnReceive (context context, Intent Intent) {Ob Ject[] Objs = (object[]) Intent.getextras (). Get ("PDUs");//Gets the received message for (Object Obj:objs) {byte[] PDU = (byte[]) obj; Smsmessage SMS = SMSMESSAGE.CREATEFROMPDU (PDU);//text message content String message = Sms.getmessagebody ();//Get the contents of the SMS LOG.D ("logo", "Message" + message);//short-term mobile phone number. +86 opening? String from = sms.getoriginatingaddress ();//Get Mobile number Toast.maketext (Getapplicationcontext (), "from:" + From, Toast.length_long). show (); LOG.D ("logo", "from" + from); Textutils.isempty (from)) {//Determine if the number is empty string code = Patterncode (message); Textutils.isempty (code)) {strcontent = Code;handler.sendemptymessage (1);}}}}; Registerreceiver (Smsreceiver, filter2);//register Broadcast Listener} @Overrideprotected void OnDestroy () {Super.ondestroy ();// Release the auto-input broadcast unregisterreceiver (smsreceiver);} /** * Matches 6 digits in the middle of the SMS (Verification code, etc.) * * @param patterncontent * @return */private string Patterncode (String patterncontent) {if (text Utils.isempty (patterncontent)) {return null;} Pattern p = pattern.compile (Patterncoder); Matcher Matcher = P.matcher (patterncontent), if (Matcher.find ()) {return Matcher.group ();} return null;}

It's also important that we give permission to our program manifest file.

<textarea readonly="" name="code" class="java"><!--SMS Receive permissions--<uses-permission android:name= "Android.permission.RECEIVE_SMS" > </uses -permission></textarea>

Then our software can automatically backfill the data!!!

Android Automatic backfill SMS verification code via broadcast

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.