Android observer ContentObserver mode get text message verification code using regular expressions

Source: Internet
Author: User

Recently, during registration, many apps directly enter the verification code when receiving text messages from their mobile phones. The user's automatic input is omitted on the interface. This is really user-friendly, so I made a new one myself.

Steps:

First, I used ContentObserver to listen for text messages. (You 'd better know the verification code sent from that number)

Then, you can get the verification code from the group that uses regular expressions in the text message (of course, the format of the verification code is required)

Paste the key code:

Register the 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; tr Y {codestr = Cus_UnitTools.getsmsyzm (Reg_ForgetPassword.this); code. setText (codestr); requestcode ();} catch (Exception e) {Log. e ("yung", "Verification code extraction 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 ;} /*** extract the verification code from the text message character seek * @ param body text message content * @ param YZMLENGTH the length of the verification code is generally 6 or 4 characters * @ return the obtained Verification Code */public static string getyzm (String body, int YZMLENGTH) {// first ([a-zA-Z0-9] {YZMLENGTH}) is to get a continuous six-digit number //(?
 


// Some verification codes are pure numbers, so you can use them directly.
// Pattern p = Pattern. compile ("(? After the listener is complete, remember getContentResolver (). unregisterContentObserver (c); cancel the listener

In this way, you can monitor the changes in the text message database. Remember to add the permission to list the text message permissions by the way.







The demo will not be written. It is very clear and simple here. Don't like it, don't spray it, there is a wrong reply, repost must respect the author-yung7086

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.