SoFunction
Updated on 2025-04-10

Android automatically obtains verification code using ContentObserver method

The two ways to automatically obtain verification codes on Android are BroadcastReceiver and ContentObserver. Both methods require two steps: registration and unregistration.
Remember to add permissions. This article introduces the ContentObserver method.

Order the ContentObserver code first

/**
 * Created by weifeiyang on 2016/7/29 0029.
 */

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

/**
  * Read the SMS verification code and set the verification code
  * Created by cool on 2016/1/4.
  */
public class ReadSmsContent extends ContentObserver {
 private Cursor cursor = null;
 private Activity mActivity;
 private EditText mEditText;

 public ReadSmsContent(Handler handler, Activity activity, EditText editText) {
  super(handler);
   = activity;
   = editText;
 }

 @Override
 public void onChange(boolean selfChange, Uri uri) {

 /*
   The first callback is not what we want. Return directly
   It is currently found that every time I receive a new text message, I will go onChange() several times,
   This method can be used to make the method in onChange only go once.
  */
  if (().equals("content://sms/raw")) {
   return;
  }

  /*
    Read text messages in your inbox

    address Sender's mobile number:
    body information content:
    read:
    date Send time:
    */
  cursor = ().query(("content://sms/inbox"),
    new String[]{"_id", "address", "body", "read"}, null, null, "_id desc");//Sort in descending order
// Specify the number//  (("content://sms/inbox"),
//    new String[]{"_id", "address", "body", "read"}, "address=? and read=?", new String[]{"10086", "0"}, "_id desc");

  if (null != cursor && () > 0) {
   ();//Point to the first place   int smsbodyColumn = ("body");//body location   String smsBody = (smsbodyColumn);//Get content   String verifyCode = getDynamicPassword(smsBody);
   if ((verifyCode)) {
    return;
   }
   if (mEditText == null) {
    throw new RuntimeException("The EditText you passed is empty");
   }
   if (("The verification code you got from the server"))) {
    (verifyCode);
    //EditText gets the focus, 3 properties must be set at the same time    (true);
    (true);
    ();
    (());//Set the cursor position   }

  }
  if (!()) {
   ();
  }
 }

 /**
   * Snap 4 consecutive digits from a string
   * Used to get dynamic passwords from text messages
   *
   * @param str SMS content
   * @return The obtained 4-digit dynamic password
   */
 public static String getDynamicPassword(String str) {
  Pattern continuousNumberPattern = ("[0-9\\.]+");
  Matcher m = (str);
  String dynamicPassword = "";
  while (()) {
   if (().length() == 4) {
    dynamicPassword = ();
   }
  }

  return dynamicPassword;
 }
}

After the observer has it, it can be used. Call initSmSContentObserver() in the onCreate method of activity or fragment to register, and pass in onDestroy()
//Login content listener
().unregisterContentObserver(readSmsContent);
Cancel Registration

 /**
   * Initialize the SMS listening database
   */
 private void initSmSContentObserver() {
  readSmsContent = new ReadSmsContent(new Handler(), this, seCodeEditText);
  //Register SMS content monitoring  ().registerContentObserver(("content://sms/"), true, readSmsContent);

 }

After obtaining SMS in the above two ways, the source account of the SMS platform is not fixed, so it is checked through the verification code.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.