当前位置: 移动技术网 > 移动技术>移动开发>Android > Android采取ContentObserver方式自动获取验证码

Android采取ContentObserver方式自动获取验证码

2019年07月24日  | 移动技术网移动技术  | 我要评论

android 自动获取验证码的两种方式分别是broadcastreceiver及contentobserver,两种方式都需要进行注册、取消注册两个步骤
记得添加权限,本文介绍contentobserver方式。

照旧先上contentobserver代码

/**
 * created by weifeiyang on 2016/7/29 0029.
 */

import android.app.activity;
import android.content.sharedpreferences;
import android.database.contentobserver;
import android.database.cursor;
import android.net.uri;
import android.os.handler;
import android.text.textutils;
import android.widget.edittext;

import java.util.regex.matcher;
import java.util.regex.pattern;

/**
 * 读取短信验证码,并设置验证码
 * created by cool on 2016/1/4.
 */
public class readsmscontent extends contentobserver {
 private cursor cursor = null;
 private activity mactivity;
 private edittext medittext;

 public readsmscontent(handler handler, activity activity, edittext edittext) {
  super(handler);
  this.mactivity = activity;
  this.medittext = edittext;
 }

 @override
 public void onchange(boolean selfchange, uri uri) {

 /*
  第一次回调 不是我们想要的 直接返回
  目前发现每次收到新短信都会走好几次onchange(),
  可以通过这个方法来使得onchange中的方法只走一次
 */
  if (uri.tostring().equals("content://sms/raw")) {
   return;
  }

  /*
   读取收件箱中的短信

   address 发件人手机号码:
   body 信息内容:
   read 是否查看:
   date 发送时间:
   */
  cursor = mactivity.getcontentresolver().query(uri.parse("content://sms/inbox"),
    new string[]{"_id", "address", "body", "read"}, null, null, "_id desc");//按降序排列

//  指定号码
//  mactivity.managedquery(uri.parse("content://sms/inbox"),
//    new string[]{"_id", "address", "body", "read"}, "address=? and read=?", new string[]{"10086", "0"}, "_id desc");

  if (null != cursor && cursor.getcount() > 0) {
   cursor.movetonext();//指向首位
   int smsbodycolumn = cursor.getcolumnindex("body");//body位置
   string smsbody = cursor.getstring(smsbodycolumn);//获取内容
   string verifycode = getdynamicpassword(smsbody);
   if (textutils.isempty(verifycode)) {
    return;
   }
   if (medittext == null) {
    throw new runtimeexception("你传的edittext为空");
   }
   if (verifycode.contains("你从服务器上获取到的验证码"))) {
    medittext.settext(verifycode);
    //edittext获取焦点,3个属性必须同时设置
    medittext.setfocusable(true);
    medittext.setfocusableintouchmode(true);
    medittext.requestfocus();
    medittext.setselection(verifycode.length());//设置光标位置
   }

  }
  if (!cursor.isclosed()) {
   cursor.close();
  }
 }

 /**
  * 从字符串中截取连续4位数字
  * 用于从短信中获取动态密码
  *
  * @param str 短信内容
  * @return 截取得到的4位动态密码
  */
 public static string getdynamicpassword(string str) {
  pattern continuousnumberpattern = pattern.compile("[0-9\\.]+");
  matcher m = continuousnumberpattern.matcher(str);
  string dynamicpassword = "";
  while (m.find()) {
   if (m.group().length() == 4) {
    dynamicpassword = m.group();
   }
  }

  return dynamicpassword;
 }
}

观察者有了后就可以使用了,在activity或者fragment的oncreate方法中调用initsmscontentobserver()进行注册,在ondestroy()中通过
//注销内容监听者
this.getcontentresolver().unregistercontentobserver(readsmscontent);
取消注册

 /**
  * 初始化短信监听数据库
  */
 private void initsmscontentobserver() {
  readsmscontent = new readsmscontent(new handler(), this, secodeedittext);
  //注册短信内容监听
  this.getcontentresolver().registercontentobserver(uri.parse("content://sms/"), true, readsmscontent);

 }

上述两种方式获取到短信后,目前由于短信平台来源账号不固定,因此是通过验证码进行校验。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网