SoFunction
Updated on 2025-04-06

Use ContentObserver mode to obtain the SMS regular automatic filling verification code

Recently, when I was registering, I saw many apps directly fill in the verification code when they received text messages on their mobile phones and omit the user's automatic input. I felt that this was really humane, so I did one myself.

step:

First I used ContentObserver to listen to text messages (it's better to know which number your verification code is sent from)

Then use regular groupings from the text message to get the verification code (of course, what format must the verification code be)

Post the key code:

Register to listen to SMS database

ContentObserver c=new ContentObserver(han) { 
        @Override 
        public void onChange(boolean selfChange) {           // TODO Auto-generated method stub 
          (selfChange); 
          (0); 
        } 
      }; 
getContentResolver().registerContentObserver(("content://sms"), true, c); 
Handler han = new Handler() { 
    @SuppressWarnings("deprecation") 
    public void handleMessage( msg) { 
      String codestr = null; 
      try { 
        codestr = Cus_UnitTools.getsmsyzm(Reg_ForgetPassword.this); 
        (codestr); 
        requestcode(); 
      } catch (Exception e) { 
        ("yung", "Verification code extraction failed:" + codestr); 
      } 
    }; 
  };  
  public static String getsmsyzm(Activity c) { 
    Uri uri = ("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 = (uri, projection, selection, selectionArgs, 
        sortOrder); 
    if(cur!=null&&()>0){ 
      (); 
      String body = (("body")).replaceAll( 
          "\n", " "); 
      (); 
      return getyzm(body, YZMLENGTH); 
  } 
    (); 
    return null; 
  } 
    /**
    * Extract verification code from SMS character tamper
    * @param body SMS content
      * @param YZMLENGTH The length of the verification code is generally 6 or 4 digits
    * @return The verification code retrieved
    */ 
  public static String getyzm(String body, int YZMLENGTH) { 
    // First ([a-zA-Z0-9]{YZMLENGTH}) is to get a continuous combination of six digit letters    // (?<![a-zA-Z0-9])Negative assertion([0-9]{YZMLENGTH}) There cannot be numbers before it    // (?![a-zA-Z0-9]) Assertion ([0-9]{YZMLENGTH}) cannot have numbers after it    Pattern p = Pattern 
        .compile("(?&lt;![a-zA-Z0-9])([a-zA-Z0-9]{" + YZMLENGTH + "})(?![a-zA-Z0-9])"); 
    Matcher m = (body); 
    if (()) { 
      (()); 
      return (0); 
    } 
    return null; 
  } 
//Some verification codes are pure numbers, so just use this directly//Pattern p = ("(?&lt;![0-9])([0-9]{" + YZMLENGTH+ "})(?![0-9])");

Remember after listeninggetContentResolver().unregisterContentObserver(c);Log out of monitoring

In this way, you can listen to changes in the SMS database. Remember to add permissions and list SMS permissions.

 &lt;!-- sending a text message--&gt;
  &lt;uses-permission Android:name=".SEND_SMS" /&gt;
  &lt;!-- Read the message --&gt;
  &lt;uses-permission android:name=".READ_SMS" /&gt;
  &lt;!-- Write a message --&gt;
  &lt;uses-permission android:name=".WRITE_SMS" /&gt;
  &lt;!-- Receive message --&gt;
  &lt;uses-permission android:name=".RECEIVE_SMS" /&gt;

The above is the editor introduced to you that the editor uses ContentObserver mode to obtain the automatic filling of the verification code for SMS in Android. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!