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.