亲宝软件园·资讯

展开

Android自动填充短信验证码 Android实现自动填充短信验证码功能

程龙_ 人气:0

前言

短信验证码获取并自动填写现在已经成为一个人性化App的标配了,这篇文章将实现一个短信验证码获取并自动填写的demo。其实就是读取指定号码的短信并提取出验证码,然后赋值给EditText显示。

demo效果图:

读取短信

Android系统在接受到一条短信的时候会发出一条Action为android.provider.Telephony.SMS_RECEIVED的有序广播,因此我们读取短信的验证码只需要监听这个广播然后提取出短信中的验证码即可,如:

/**
* Created by 程龙 on 2018/9/27.
*/

public class SMSBroadcastReceiver extends BroadcastReceiver {

 private static final String TAG = "SMSBroadcastReceiver";
 private OnReceiveSMSListener mOnReceiveSMSListener;
 public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";

 @Override
 public void onReceive(Context context, Intent intent) {
  if (intent.getAction().equals(SMS_RECEIVED_ACTION)) {
   Object[] pdus = (Object[]) intent.getExtras().get("pdus");
   for(Object pdu:pdus) {
    SmsMessage smsMessage = SmsMessage.createFromPdu((byte [])pdu);
    // 短信号码
    String sender = smsMessage.getDisplayOriginatingAddress();
    Log.d(TAG,sender+"");
    //短信内容
    String content = smsMessage.getDisplayMessageBody();
    // 筛选
    if ("短信发送方号码".equals(sender) && mOnReceiveSMSListener!= null) {
     Pattern pattern = Pattern.compile("\\d+");
     Matcher matcher = pattern.matcher(content);
     if(matcher.find()) {
      mOnReceiveSMSListener.onReceived(matcher.group());
     }
     abortBroadcast();
    }
   }
  }

 }

 /**
 * 回调接口
 */
 public interface OnReceiveSMSListener {
  void onReceived(String message);
 }


 public void setOnReceiveSMSListener(OnReceiveSMSListener onReceiveSMSListener) {
  mOnReceiveSMSListener = onReceiveSMSListener;
 }
}

代码注释比较清楚,通过SmsMessage对象可以得到短信的发送号码,短信内容及相关信息。当短信号码为你要读取的短信发送方号码时用正则表达式提取出短信内容的验证码,然后通过调用监听器的回调方法更新相应的UI。这里只需将短信验证发送方号码替换成相应的号码即可,如“106903561008710”为腾讯云注册发送的短信号码。

通常,我们只会在注册或登录页面读取短信验证码,所以,我们采用动态注册广播的方式注册这个广播,如:

// 注册广播
IntentFilter intentFilter = new IntentFilter(SMSBroadcastReceiver.SMS_RECEIVED_ACTION);
// 设置优先级
intentFilter.setPriority(Integer.MAX_VALUE);
registerReceiver(mSMSBroadcastReceiver,intentFilter);

取消注册

@Override
protected void onDestroy() {
 super.onDestroy();
 unregisterReceiver(mSMSBroadcastReceiver);
}

MainActivity.java的代码为:

public class MainActivity extends AppCompatActivity implements SMSBroadcastReceiver.OnReceiveSMSListener {

 private SMSBroadcastReceiver mSMSBroadcastReceiver = new SMSBroadcastReceiver();
 private EditText mEt_code;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mEt_code = (EditText) findViewById(R.id.et_code);
  mSMSBroadcastReceiver.setOnReceiveSMSListener(this);
  // 注册广播
  IntentFilter intentFilter = new IntentFilter(SMSBroadcastReceiver.SMS_RECEIVED_ACTION);
  // 设置优先级
  intentFilter.setPriority(Integer.MAX_VALUE);
  registerReceiver(mSMSBroadcastReceiver,intentFilter);
 }

 @Override
 public void onReceived(String message) {
  mEt_code.setText(message+"");
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  unregisterReceiver(mSMSBroadcastReceiver);
 }
}

这个demo的代码实现比较简单,就是监听广播,读取收到的短信,然后过滤筛选出我们要的验证码短信,再用正则表达式提取到验证码设置给EditText显示即可。大家下载或者自己编写这个demo并更改发送方短信号码运行,然后可以找个注册网址或是模拟验证码短信进行测试。

加载全部内容

相关教程
猜你喜欢
用户评论