Android 获取短信验证码,自动填充

1、申请权限

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

API>23动态申请权限

private static final String[] authBaseArr = {//申请类型Manifest.permission.RECEIVE_SMS,Manifest.permission.READ_SMS,
};
private static final int authBaseRequestCode = 1;
private boolean hasBasePhoneAuth() {PackageManager pm = getPackageManager();for (String auth : authBaseArr) {if (pm.checkPermission(auth, getPackageName()) != PackageManager.PERMISSION_GRANTED) {return false;}}return true;
}private void initNavi() {// 申请权限if (android.os.Build.VERSION.SDK_INT >= 23) {if (!hasBasePhoneAuth()) {this.requestPermissions(authBaseArr, authBaseRequestCode);return;}}
}

2、自定义ContentObserver

/*** 作者:created by meixi* 邮箱:1085220040@qq.com* 日期:2019/12/11 17:37*/
public class SmsObserver extends ContentObserver {private Context mContext;private Handler mHandler;private int mReceivedCode = 1;public SmsObserver(Context context, Handler handler, int received_code) {super(handler);mContext = context;mHandler = handler;mReceivedCode = received_code;}@Overridepublic void onChange(boolean selfChange, Uri uri) {super.onChange(selfChange, uri);String code = "";if (uri.toString().equals("content://sms/raw")) {return;}Uri inboxUri = Uri.parse("content://sms/inbox");final String[] projection = new String[]{"address", "body"};Cursor c = mContext.getContentResolver().query(inboxUri, projection, "read=?", new String[]{"0"}, "date desc");
//        Cursor c = mContext.getContentResolver().query(inboxUri, null, null, null, "date desc");if (c != null) {if (c.moveToFirst()) {String address = c.getString(c.getColumnIndex("address"));String body = c.getString(c.getColumnIndex("body"));LgqLogutil.e("发件人为:" + address + "》》》》短信内容为:" + body);Pattern pattern = Pattern.compile("(\\d{4,6})");Matcher matcher = pattern.matcher(body);if (matcher.find()) {code = matcher.group(0);LgqLogutil.e("验证码》》》" + code);ClipboardManager cmb = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);cmb.setText(code);mHandler.obtainMessage(mReceivedCode, code).sendToTarget();}}c.close();}}
}

3、activity实现代码

    private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {if (msg.what == 1) {String code = (String) msg.obj;Log.e("lgq","短信===="+code);
//                mEtUserVerificationCode.setText(code);//填充}}};
SmsObserver mObserver = new SmsObserver(this, mHandler, 1);Uri uri = Uri.parse("content://sms");
getContentResolver().registerContentObserver(uri, true, mObserver);

附:获取首条短信

        String code = "";//        Uri inboxUri = Uri.parse("content://sms/inbox");Uri inboxUri = Uri.parse("content://sms");Cursor c = getContentResolver().query(inboxUri, null, null, null, "");if (c != null) {if (c.moveToFirst()) {
//            if (c.moveToNext()) {String address = c.getString(c.getColumnIndex("address"));String body = c.getString(c.getColumnIndex("body"));LgqLogutil.i("main发件人为:" + address + "》》》》短信内容为:" + body);Pattern pattern = Pattern.compile("(\\d{4,6})");Matcher matcher = pattern.matcher(body);if (matcher.find()) {code = matcher.group(0);LgqLogutil.e("main验证码》》》" + code);ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);cmb.setText(code);mHandler.obtainMessage(mReceivedCode, code).sendToTarget();}}c.close();}

 

 

注:有这个防护标志的,会获取失败

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/414434.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

spring-boot-starter-parent

一、你的项目 pom.xml 中有这段代码吗 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath/> </parent>idea &…

项目集成Spring Security

前言 之前写的 涂涂影院管理系统 这个 demo 是基于 shiro 来鉴权的&#xff0c;项目前后端分离后&#xff0c;显然集成 Spring Security 更加方便一些&#xff0c;毕竟&#xff0c;都用 Spring 了&#xff0c;权限管理当然 Spring Security. 花了半天时间整理的笔记&#xff0…

android 使用AIDL实现进程间通讯

一、创建服务端 1、首先创建AIDL文件 2、创建service&#xff0c;绑定AIDL接口 3、配置service <service android:name".AidlService"android:enabled"true"android:exported"true"><intent-filter android:priority"1000"&…

大数据入门第二十天——scala入门(二)scala基础02

一、 类、对象、继承、特质 1.类 Scala的类与Java、C的类比起来更简洁 定义&#xff1a; package com.jiangbei //在Scala中&#xff0c;类并不用声明为public。 //Scala源文件中可以包含多个类&#xff0c;所有这些类都具有公有可见性。 class Person {// 定义一个不可变的val…

SpringSecurity 整合 JWT

项目集成Spring Security&#xff08;一&#xff09; 在上一篇基础上继续集成 JWT &#xff0c;实现用户身份验证。 前言 前后端分离项目中&#xff0c;如果直接把 API 接口对外开放&#xff0c;我们知道这样风险是很大的&#xff0c;所以在上一篇中我们引入了 Spring Securit…

Spring Security 认证执行流程

本文基于 Spring Security 5.x 推荐阅读&#xff1a; 项目集成Spring Security SpringSecurity 整合 JWT 一、外层-正常登陆调用 项目启动后会自动寻找 UserDetailsService 实现类&#xff1b; 执行 UserDetailsService 的唯一方法 loadUserByName(String username) 并返回…

ScrollView嵌套ViewPager,ViewPage动态设置高度,嵌套事件冲突——滑动冲突解决方法

1、创建自定义ViewPager public class ViewPagerForScrollView extends ViewPager {int myh0 ;boolean ifme false;public ViewPagerForScrollView(Context context) {super(context);Log.i("lgq","高度111 " );}public ViewPagerForScrollView(Context c…

centos7下python3与python2共存并且开启py3虚拟环境

因为下载视频需要用到python3环境&#xff0c;今天在我的win上安装下载工具死活安装不上去&#xff0c;在大盘鸡上一下就安装成功了...可能在win上不兼容吧...无奈只能在大盘鸡上进行折腾了&#xff0c;顺便几个笔记 由于大盘鸡上好多程序基于python2的&#xff0c;所以就没法直…

Unable to add window token null is not valid is your activity running

莫名其妙的报错 错误原因是不在主线程调用了弹窗方法 解决方法很简单 创建Handler static final int REFRESH_COMPLETE 0X1112; private Handler mHandler new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case REFRESH_COMPLETE:Ob…

01-Flutter移动电商实战-项目学习记录

01-Flutter移动电商实战-项目学习记录 一直想系统性的学习一下 Flutter&#xff0c;正好看到该课程《Flutter移动电商实战》的百度云资源&#xff0c;共 69 课时&#xff0c;由于怕自己坚持不下去&#xff08;经常学着学着就不学了&#xff09;&#xff0c;故采用博客监督以记之…