Android实现自动填写获取验证码功能
程序员文章站
2024-02-27 13:19:27
本文实例讲解了android自动提取短信验证码解决方案,分享给大家供大家参考,具体内容如下
本文实例讲解了android自动提取短信验证码解决方案,分享给大家供大家参考,具体内容如下
<uses-permission android:name="android.permission.receive_sms"></uses-permission> <uses-permission android:name="android.permission.read_sms"></uses-permission>
package com.dongdongsongcan.app; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.telephony.smsmessage; import com.su.dolphin.utils.logutil; import java.util.arraylist; import java.util.list; import java.util.regex.matcher; import java.util.regex.pattern; public class smsreceiver extends broadcastreceiver { public interface ismslistener { public void onsmsreceive(string verifycode); } private static ismslistener msmslistener; public smsreceiver(ismslistener ismslistener) { msmslistener = ismslistener; } public static final string tag = "imichatsmsreceiver"; //android.provider.telephony.sms.intents public static final string sms_received_action = "android.provider.telephony.sms_received"; @override public void onreceive(context context, intent intent) { logutil.d(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); if (intent.getaction().equals(sms_received_action)) { smsmessage[] messages = getmessagesfromintent(intent); for (smsmessage message : messages) { // logutil.d(message.getoriginatingaddress() + " : " + // // message.getdisplayoriginatingaddress() + " : " + // // message.getdisplaymessagebody() + " : " + // // message.gettimestampmillis()); string msg = message.getdisplaymessagebody(); logutil.d("msg: " + msg); string verifycode = null; pattern p = pattern.compile("\\d{4}"); matcher m = p.matcher(msg); while (m.find()) { verifycode = m.group(); break; } logutil.d("verifycode " + verifycode); if (msmslistener != null) { msmslistener.onsmsreceive(verifycode); } } } } public final smsmessage[] getmessagesfromintent(intent intent) { object[] messages = (object[]) intent.getserializableextra("pdus"); byte[][] pduobjs = new byte[messages.length][]; for (int i = 0; i < messages.length; i++) { pduobjs[i] = (byte[]) messages[i]; } byte[][] pdus = new byte[pduobjs.length][]; int pducount = pdus.length; smsmessage[] msgs = new smsmessage[pducount]; for (int i = 0; i < pducount; i++) { pdus[i] = pduobjs[i]; msgs[i] = smsmessage.createfrompdu(pdus[i]); } return msgs; } }
以上就是本文的全部内容,希望对大家的学习有所帮助。
上一篇: css 之 background-position-x
下一篇: CSS2实现的隔行换色