android之实现验证码的自动回填

来源:互联网 发布:在知乎匿名回答问题 编辑:程序博客网 时间:2024/05/17 03:24

本文主要是记录一些零碎的知识点

获取短信验证码,并实现自动回填,短信涉及隐私,以及无法随便读取了,注册广播肯定不能正常工作,以下只是我个人的解决方案哈,不代表唯一解决方案(仍然需要得到用户的权限许可)

具体做法是:记录用户点击获取验证的时间,都短信库,最新的一条是如果一分钟以内的,就获取其中的验证码,如果没有找到,就开个线程,一直循环读取短信库。

首先先添加权限 AndroidManifest.xml

<!-- 读取短信 -->    <uses-permission android:name="android.permission.READ_SMS" />
哈哈,获取验证码前先看看手机号格式对不对

/**     * 验证手机号是否符合大陆的标准格式     */    public static boolean isMobileNumberValid(String mobiles) {//        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");        Pattern p = Pattern.compile("^(1[3|4|5|7|8])\\d{9}$");        Matcher m = p.matcher(mobiles);        return m.matches();    }
还需要这样一个界面



<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.cl.android.tse01.LoginActivity"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="登录界面,可以放张图片" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <android.support.design.widget.TextInputLayout            android:layout_width="match_parent"            android:layout_weight="1"            android:layout_height="wrap_content"            android:layout_marginTop="16dp">            <EditText android:id="@+id/verify_mobileNumber"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:hint="手机号"                android:imeOptions="actionUnspecified"                android:inputType="phone"                android:maxLines="1"                android:singleLine="true" />        </android.support.design.widget.TextInputLayout>        <Button android:id="@+id/resend"            style="?android:textAppearanceSmall"            android:layout_width="fill_parent"            android:layout_weight="2"            android:layout_height="wrap_content"            android:layout_marginTop="16dp"            android:text="获取验证码"            android:textStyle="bold" />    </LinearLayout>    <android.support.design.widget.TextInputLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="16dp">        <EditText android:id="@+id/sms_code"            android:layout_height="wrap_content"            android:layout_width="match_parent"            android:layout_weight="2"            android:hint="验证码"            android:imeOptions="actionUnspecified"            android:layout_marginTop="16dp"            android:maxLines="1"            android:singleLine="true" />    </android.support.design.widget.TextInputLayout>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:id="@+id/verify_login"        android:text="登录"        android:textStyle="bold"/></LinearLayout>
同上一篇一样,有个design工具包需要导入

想实现用户点击获取验证码后60s内无法再次点击,按钮显示的是60s倒计时,这60s内会读取短信的内容,看看有没有一分钟到达的短信,先看倒计时的实现

private Handler handler;private long sendtime; //用户点击发送验证码的时间private Uri SMS_INBOX ;//短信箱,读取短信需要private long sendtime; //用户点击发送验证码的时间handler = new Handler();SMS_INBOX = Uri.parse("content://sms/");//用户点击获取验证码时 ,变灰+倒计时handler.post(readSMS);    <span style="white-space:pre"></span>endtime = System.currentTimeMillis();//这里是 获取短信验证码的操作。。。    <span style="white-space:pre"></span>timecount = 10;//60s    <span style="white-space:pre"></span>handler.post(timeCountdown);//60s 倒计时内,获取验证码不可点击Runnable timeCountdown = new Runnable() {        @Override        public void run() {            Log.i(TAG, "handler running......");            //1s更新一次            if (timecount > 0) {                resend.setEnabled(false);                timecount--;                resend.setText(timecount + "s");                handler.postDelayed(timeCountdown, 1000);            } else {                resend.setEnabled(true);                resend.setText("获取验证码");            }        }    };
读取短信的方法,验证码时6位连续的数字

/**     * 自动填写验证码     * 用正则表达式截取,String patternCoder = "(?<!\\d)\\d{6}(?!\\d)"。     * 如果广播接收不到怎么办,毕竟涉及隐私呀,API的关于SMS的都已废弃     * 还是读取收件箱最新的消息吧,但是我不能一直读取吧,怎么收到刚发的短信     * 格1s读取一下?开个线程吧     * 记录一下发送的时间,接收的时间在发送的时间一分钟以内就算是刚收到的验证短信     */    public String getSmsFromPhone() {        //String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };        String[] projection = new String[] { "date","body" };        String where = " date >  " + (sendtime -  60 * 1000);//60s以内的短信        //先记录发送命令的时间,找这时间以后收到的短信  ,按时间降序排序        Cursor cur = getContentResolver().query(SMS_INBOX, projection, where, null, "date desc");        Log.i(TAG,"cur..........."+(sendtime -  60 * 1000));        if (null == cur) {            Log.i(TAG, "cur = null..........");            return null;        }        // 1458371840372   1458365678000        //只要一条,使用if,如果是读取所有的短信,使用while        if (cur.moveToNext()) {            String date = cur.getString(cur.getColumnIndex("date"));            String body = cur.getString(cur.getColumnIndex("body"));            Log.i(TAG, "date:   ......"+date+"body:"+body);            //这里我是要获取短信中的验证码            Pattern pattern = Pattern.compile("(?<!\\d)\\d{6}(?!\\d)");            Matcher matcher = pattern.matcher(body);            if (matcher.find()) {                String res = matcher.group(0).substring(0,6);                Log.i(TAG,res+"..........");                return res;            }        }        return null;    }
在看看如何循环读取短信库,获取符合条件的信息的Runnable

//读取短信Runnable readSMS  = new Runnable() {        @Override        public void run() {            //读取短信内容            if(getSmsFromPhone() == null){                handler.postDelayed(readSMS, 1000);            }else{                smsCode.setText(getSmsFromPhone());            }        }    };

亲测可行,只是一个简单的读取短信的权限,目前的系统下可行


0 0
原创粉丝点击