Android自动获取短信验证码

Posted 王永迪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android自动获取短信验证码相关的知识,希望对你有一定的参考价值。

一、工具封装

public class GetCodeUtil extends ContentObserver 

    public static final String SMS_URI_INBOX = "content://sms/inbox";
    private Activity activity = null;
    private EditText verifyText = null;

    public SmsReadUtil(Activity activity, Handler handler, EditText verifyText) 
        super(handler);
        this.activity = activity;
        this.verifyText = verifyText;
    

    @Override
    public void onChange(boolean selfChange) 
        super.onChange(selfChange);
        try 
            Uri uri = Uri.parse(SMS_URI_INBOX);
            String[] projection = new String[]"_id", "address", "body", "read";

            // 读取指定号码的短信
            //Cursor cursor = activity.getContentResolver()
            //                        .query(uri, projection, "address=? and read=?", 
            //                        new String[]"1069036136580", "0", "date desc");

            // 获取非指定号码短信 
            Cursor cursor = activity.getContentResolver()
                                    .query(uri, projection, null, null, "date desc");
            // 如果短信为未读模式
            if (cursor != null) 
                cursor.moveToFirst();
                if (cursor.moveToFirst()) 
                    String smsbody = cursor.getString(cursor.getColumnIndex("body"));
                    String smsContent = getAuthCode(smsbody);
                    if (!TextUtils.isEmpty(smsContent)) 
                        verifyText.setText(smsContent);
                        verifyText.setSelection(smsContent.length());
                    
                
                cursor.close();
            
         catch (SecurityException e) 
            e.printStackTrace();
            Logger.e("read sms exception = " + e.getMessage());
        
    

    /**
     * @param content 短信内容
     * @return 截取得到的6位动态密码
     */
    public String getAuthCode(String content) 
        // 特殊校验,在为设置拦截固定号码时,可能拦截其他验证码,如果设置固定号码时,将此注释掉
        if (!content.contains("康小白")) 
            return null;
        
        Pattern continuousNumberPattern = Pattern.compile("[0-9\\\\.]+");
        Matcher m = continuousNumberPattern.matcher(content);
        String authCode = "";
        while (m.find()) 
            if (m.group().length() == 6) 
                authCode = m.group();
            
        
        return authCode;
    

2.设置监听 Activity oncreate方法即可

        // 注册短信变化监听
        smsReadUtil = new SmsReadUtil(MineLoginActivity.this, new Handler(), editCode);
        getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, smsReadUtil);

3.取消注册

    @Override
    protected void onDestroy() 
        super.onDestroy();
        ContentResolver contentResolver = getContentResolver();
        if (contentResolver != null) 
            contentResolver.unregisterContentObserver(smsReadUtil);
        
    

以上是关于Android自动获取短信验证码的主要内容,如果未能解决你的问题,请参考以下文章

Android自动获取短信验证码

python+appium自动化测试获取短信+图片验证码

复习python接口自动化中获取短信验证码的方法

自动获取短信验证码

Android实现阿里云短信业务获取验证码-后端SSM+redis

利用CursorLoader实现短信验证码自动填写