Android realizes SMS verification code automatic filling function

  • 2020-12-07 04:24:36
  • OfStack

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

The idea is simple:

1. Register to listen to SMS broadcasts in Activity code that requires input of captcha 2. Intercept SMS messages and get the captCHA 3. Write back to EditText

private SmsReciver smsReciver = new SmsReciver(); 
/**  Receive a message Action **/ 
    String ACTION_SMS_RECIVER = "android.provider.Telephony.SMS_RECEIVED"; 
  /** 
   *  The registered broadcast receiver listens to the SMS verification code automatically writes back   Can be in onCreate() Registration in progress ; 
   */ 
  private void registSmsReciver() { 
    IntentFilter filter = new IntentFilter(); 
    filter.addAction(ACTION_SMS_RECIVER); 
    //  Set priorities   Or you won't get a text message   
    filter.setPriority(1000); 
    registerReceiver(smsReciver, filter); 
    } 
 
     /** 
    *  SMS broadcast receiver   The user listens to the message and fills in the verification code automatically  
    */ 
     private class SmsReciver extends BroadcastReceiver { 
 
    @Override 
    public void onReceive(Context context, Intent intent) { 
      Object[] objs = (Object[]) intent.getExtras().get("pdus"); 
       for (Object obj : objs) { 
         yte[] pdu = (byte[]) obj; 
         SmsMessage sms = SmsMessage.createFromPdu(pdu); 
         //  The content of the message  
         String message = sms.getMessageBody(); 
         Log.d("log", "message   " + message); 
        //  SMS mobile phone number, if your company sent the verification code number is fixed here can be carried out 1 A check of the number  
         String from = sms.getOriginatingAddress(); 
         Log.d("log", "from   " + from); 
         analysisVerify(message); 
 
      } 
     } 
 
  } 
 
   /** 
   *  Parse the message and write back   This is a purely numeric text message. If the partner's captcha contains letters, it can be replaced with a regular one  
   * 
   * @param message 
   */ 
  private void analysisVerify(String message) { 
    char[] msgs = message.toCharArray(); 
     StringBuffer sb = new StringBuffer(); 
    for (int i = 0; i < msgs.length; i++) { 
      if ('0' <= msgs[i] && msgs[i] <= '9') { 
              sb.append(msgs[i]); 
      } 
    } 
 
    mEtVerifyCode.setText(sb.toString()); 
   } 
 
  @Override 
  protected void onDestroy() { 
    super.onDestroy(); 
    //  Cancel SMS broadcast registration  
    if (smsReciver != null) { 
      unregisterReceiver(smsReciver); 
      smsReciver = null; 
    } 
  } 

As you can see, the code logic is relatively simple, and there are a few points to note. The code we use here registers for broadcast. The reason why we don't use global broadcast is that for two days, in the higher version of api, the registered global SMS listening will become invalid. In addition, as far as business is concerned, we only use SMS monitoring in Activity when we input captchas. We use the form of code registration to cancel the broadcast registration when Activity is destroyed, which is more in line with our expectations and improves the performance of the application.

The second issue that needs attention is the issue of priorities.


filter.setPriority(1000); 

As you can see, we've set the priority to maximum here. Make sure that our app can receive SMS messages as much as possible. Note that I'm using "as far as possible," which means that there is no guarantee that SMS autofill 1 will be successful. A friend might ask, "Didn't we set our priority to the highest?"

Why can't it be guaranteed?

The reason is simple: you can set the priority of listening to text messages to the maximum, just as other apps can set the priority of listening to text messages to the maximum. For example, if you have 360 security guards installed on your phone and your company's captcha is blocked as spam, then SMS blocking will be ineffective.

Above is the entire content of this article, I hope to help you with your study.


Related articles: