Android实现自动填写获取验证码功能
作者:lijiao 时间:2023-04-17 13:47:12
本文实例讲解了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;
}
}
标签:android,验证码
0
投稿
猜你喜欢
Intellij IDEA导入JAVA项目并启动(图文教程)
2021-11-05 12:30:18
SpringBoot实现单文件上传
2023-10-01 21:43:42
Springcloud-nacos实现配置和注册中心的方法
2023-06-15 13:46:42
Android实现关机与重启的几种方式(推荐)
2022-08-31 14:29:54
c#操作Redis的5种基本类型汇总
2021-06-03 03:24:07
Java异常分类及统一处理详解
2022-01-27 12:32:24
深入解析Java的设计模式编程中建造者模式的运用
2022-08-10 23:31:22
可伸缩的textview详解(推荐)
2021-11-06 00:34:51
java基于JDBC连接Oracle 11g Release2实例分析
2022-06-06 02:36:36
Android studio 快捷键大全
2022-03-27 14:58:56
C# 基础入门--关键字
2023-01-22 15:44:23
Java常用类库Apache Commons工具类说明及使用实例详解
2021-06-08 12:34:17
使用Jitpack发布开源Java库的详细流程
2021-12-18 06:55:58
实例讲解Android多线程应用开发中Handler的使用
2021-11-23 03:23:13
SpringCloud微服务熔断器Hystrix使用详解
2023-07-06 22:57:46
SpringDataJPA在Entity中常用的注解介绍
2023-11-27 09:40:36
C# 批处理调用方法
2021-06-18 17:14:19
Java实战之用springboot+netty实现简单的一对一聊天
2023-12-03 07:28:19
Java中常见的查找算法与排序算法总结
2021-07-14 13:21:23
DWR异常情况处理常见方法解析
2022-10-14 02:10:50