Automatically obtain the SMS verification code and SMS Verification Code

Source: Internet
Author: User

Automatically obtain the SMS verification code and SMS Verification Code

1. Custom listening class

/*** Sms listener, used to automatically fill in the Verification Code */public class SMSContentObserver extends ContentObserver {public final String SMS_URI_INBOX = "content: // sms/inbox "; // inbox private Activity activity = null; private String smsContent = ""; // Verification Code private EditText verifyText = null; // Verification Code edit box private String SMS_ADDRESS_PRNUMBER = "10690329013589 "; // The private String smsID = ""; // when the text message observer receives a text message, the onchange method is executed twice, so the SMS id is compared. If they are consistent, the public message is not processed. SMSContentObserver (Activity activity, Handler handler, EditText verifyText) {super (handler); this. activity = activity; this. verifyText = verifyText;} @ Overridepublic void onChange (boolean selfChange) {super. onChange (selfChange); Cursor cursor = null; // The cursor // read the text message Cursor = activity of the specified number in the inbox. getContentResolver (). query (Uri. parse (SMS_URI_INBOX), new String [] {"_ id", "address", "body", "read"}, // attribute to be read "addres S =? And read =? ", // What are the query conditions? new String [] {SMS_ADDRESS_PRNUMBER," 0 "}, // the query condition value is" date desc "); // sort if (cursor! = Null) {cursor. moveToFirst (); if (cursor. moveToFirst () {// compare to whether the ID of the last received SMS is equal if (! SmsID. equals (cursor. getString (cursor. getColumnIndex ("_ id") {String smsbody = cursor. getString (cursor. getColumnIndex ("body"); // use a regular expression to match the verification code Pattern pattern = Pattern. compile ("[0-9] {6}"); Matcher matcher = pattern. matcher (smsbody); if (matcher. find () {// match the 6-digit Verification Code smsContent = matcher. group (); if (verifyText! = Null & null! = SmsContent &&! "". Equals (smsContent) {verifyText. requestFocus (); // obtain verifyText. setText (smsContent); // sets the text verifyText. setSelection (smsContent. length (); // set the cursor position} smsID = cursor. getString (cursor. getColumnIndex ("_ id "));}}}}}

2. Event-based listening class on the login page

// Instantiate SMS listener SMSContentObserver mObserver = new SMSContentObserver (getActivity (), new Handler (), mEt_auth_code); // register SMS change listener mContext. getContentResolver (). registerContentObserver (Uri. parse ("content: // sms/"), true, mObserver );

3. Permission to read text messages

<uses-permission android:name="android.permission.RECEIVE_SMS" /><uses-permission android:name="android.permission.READ_SMS" /><uses-permission android:name="android.permission.WRITE_SMS" />

4. Remember to log out of the listener to prevent memory leakage

@ Overridepublic void onDestroy () {super. onDestroy (); // deregister the text message to listen to mContext. getContentResolver (). unregisterContentObserver (mObserver );}

 

Summary:

Retrieving text messages from the SMS library is not easy to intercept.

  

  

  

  

 

Related Article

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.