Android实现自动填充短信验证码功能
程序员文章站
2022-04-30 22:43:56
前言
短信验证码获取并自动填写现在已经成为一个人性化app的标配了,这篇文章将实现一个短信验证码获取并自动填写的demo。其实就是读取指定号码的短信并提取出验证码,然后赋...
前言
短信验证码获取并自动填写现在已经成为一个人性化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并更改发送方短信号码运行,然后可以找个注册网址或是模拟验证码短信进行测试。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。