Android Contentobserver mode to get SMS with regular self-actively fill the verification code

Source: Internet
Author: User

The recent time to do a register to see a lot of apps in the phone to receive text messages directly to fill out the verification code to the interface to omit the user's own active input, it is really quite human-like. So I also made a

Steps:

First I use Contentobserver to listen to SMS, (it is better to know that your verification code from that number sent over)

Then get the verification code from the regular packet in the SMS (of course, what format the verification code must be)

Post the key code:

Register to monitor SMS database

Contentobserver c=new Contentobserver (han) {@Overridepublic void OnChange (Boolean selfchange) {//TODO auto-generated Method Stubsuper.onchange (Selfchange); han.sendemptymessage (0);}}; Getcontentresolver (). Registercontentobserver (Uri.parse ("Content://sms"), True, C); Handler Han = new Handler () {@SuppressWarnings ("deprecation") public void Handlemessage (Android.os.Message msg) {String Codestr = null;try {codestr = Cus_unittools.getsmsyzm (reg_forgetpassword.this); Code.settext (CODESTR); Requestcode ();} catch (Exception e) {log.e ("Yung", "Authenticode Fetch failed:" + codestr);}};}; public static String Getsmsyzm (Activity c) {uri uri = uri.parse ("Content://sms/inbox"); string[] projection = new string[] {"Address", "person", "body"}; String selection = "address= '" + Jtphone + "'"; string[] Selectionargs = new string[] {}; String sortOrder = "Date desc"; @SuppressWarnings ("deprecation") Cursor cur = c.managedquery (URI, projection, selection, Selectionargs,sortorder); if (Cur!=null&&cur.getcount () >0) {Cur.moVetofirst (); String BODY = cur.getstring (Cur.getcolumnindex ("Body")). ReplaceAll ("\ n", ""); Cur.close (); return Getyzm (Body, Yzmlength);}        Cur.close (); return null;} /** * FROM SMS character channeling extract verification code * @param body SMS content * @param yzmlength Verification Code length general 6-bit or 4-bit * @return Authentication code */public static Strin G Getyzm (String body, int yzmlength) {//First ([A-za-z0-9]{yzmlength}) is to get a continuous six-digit alphanumeric combination//(? <![ A-ZA-Z0-9]) negative assertion ([0-9]{yzmlength}) cannot have a number in front of//(?

! [A-za-z0-9]) The assertion ([0-9]{yzmlength}) cannot have a number after the pattern P = pattern. Compile (? <![ A-ZA-Z0-9]) ([a-za-z0-9]{"+ Yzmlength +"}) (?! [a-za-z0-9]); Matcher m = p.matcher (body), if (M.find ()) {System.out.println (M.group ()); return M.group (0);} return null;}


Some codes are purely digital, so you can use this directly.
Pattern p = pattern.compile ("(? <![ 0-9]) ([0-9]{"+ yzmlength+"}) (?

! [0-9]);
After listening, remember Getcontentresolver (). Unregistercontentobserver (c); logoff monitoring

This way, you can hear the changes in the SMS database. Remember that you also need to add permissions by listing text message permissions

<!--send SMS--

<uses-permission android:name= "Android.permission.SEND_SMS"/>
<!--reading messages--
<uses-permission android:name= "Android.permission.READ_SMS"/>
<!--writing messages--
<uses-permission android:name= "Android.permission.WRITE_SMS"/>
<!--receive messages--
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>

Demo will not write it is very clear here and very easy. Do not like to spray. Wrong reply. Reprint must respect author-yung7086

Android Contentobserver mode to get SMS with regular self-actively fill the verification code

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.