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