SoFunction
Updated on 2025-03-11

Android realizes automatic filling of SMS verification code

This article shares the specific code of Android's automatic filling of SMS verification code for your reference. The specific content is as follows

SMS verification code is a function that exists in most software. At the same time, in order to avoid tedious operations caused by users' input, some app designers set it to an automatic filling method to facilitate users' operations. So what is the implementation of this method?

Use broadcast receiver to intercept SMS to obtain matching content, provide back, expose SMS content to activity to achieve automatic filling

First we need to implement a broadcast receiver

package ;

import ;
import ;

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

public class SmsReciver extends BroadcastReceiver {
 private String patternCoder = "(?<!\\d)\\d{6}(?!\\d)";
 @Override
 public void onReceive(Context context, Intent intent) {
  //Get SMS data  Object[] objs = (Object[]) ().get("pdus");
  for (Object obj : objs) {
   byte[] pdu = (byte[]) obj;
   //Encapsulate the byte array into a smsmessage object   SmsMessage sms = (pdu);
   //Get short message content   String message = ();
   ("SMS content", "message:" + message);
   // SMS number.  .  Starting with +86?   String from = ();
   ("Source of SMS", "from :" + from);
   if (!(from)) {
    String code = patternCode(message);
    if (!(code)) {
     (code);
    }
   }
  }
 }
 /**
   * Match 6 numbers in the middle of the text message (verification code, etc.)
   *
   * @param patternContent
   * @return
   */
 private String patternCode(String patternContent) {
  if ((patternContent)) {
   return null;
  }
  Pattern p = (patternCoder);
  Matcher matcher = (patternContent);
  if (()) {
   return ();
  }
  return null;
 }

 // Callback interface public interface MessageListener {
  public void onReceived(String message);
 }

 MessageListener mMessageListener;

 public void setOnReceivedMessageListener(MessageListener messageListener) {
   = messageListener;
 }
}

ok we have implemented the broadcast receiver above. In the activity, we want to bind an intent filter and register this broadcast in the destory method to unregister it.

package ;

import ;
import ;
import ;

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

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

/**
  * Change password
  *
  * @author zhaomy
  *
  */
public class RestartLoginOrTradPwdActivity extends MyBaseActivity {

 SmsReciver reciver;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  (savedInstanceState);
  setContentView(.activity_restart_login_or_trad);

  init();
 }

 private void init() {
  code = (EditText) findViewById(.register_code);
  reciver=new SmsReciver();
  IntentFilter filter = new IntentFilter();
  // Set SMS interception parameters  (".SMS_RECEIVED");
  //Set the maximum priority  (Integer.MAX_VALUE);
  registerReceiver(reciver, filter);
  (new MessageListener() {

   @Override
   public void onReceived(String message) {
    (message);
   }
  });
 }
 @Override
 protected void onPause() {
  ();
 }
 @Override
 protected void onDestroy() {
  unregisterReceiver(reciver);
  ();
 }
}

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.