AndroID 短信验证码自动填写
1.自定义Observer监听短信数据库变化(注意添加短信权限)
import androID.app.Activity;import androID.content.ContentResolver;import androID.database.ContentObserver;import androID.database.Cursor;import androID.net.Uri;import androID.os.Handler;import androID.text.TextUtils;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Created by 001 on 17/3/22. */public class SmsObserver extends ContentObserver { public static final String SMS_URI_INBox = "content://sms/inBox"; private Activity activity = null; private String smsContent = ""; private SmsListener Listener; public SmsObserver(Activity activity,Handler handler,SmsListener Listener) { super(handler); this.activity = activity; this.Listener = Listener; } @OverrIDe public voID onChange(boolean selfChange) { super.onChange(selfChange); Cursor cursor = null; // 读取收件箱中含有某关键词的短信 ContentResolver contentResolver = activity.getContentResolver(); cursor = contentResolver.query(Uri.parse(SMS_URI_INBox),new String[] { "_ID","address","body","read" },"body like ? and read=?",new String[] { "%关键词%","0" },"date desc"); if (cursor != null) { cursor.movetoFirst(); if (cursor.movetoFirst()) { String smsbody = cursor .getString(cursor.getColumnIndex("body")); String regEx = "[^0-9]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(smsbody.toString()); smsContent = m.replaceAll("").trim().toString(); if (!TextUtils.isEmpty(smsContent)) { Listener.onResult(smsContent); } } } } /* * 短信回调接口 */ public interface SmsListener { /** * 接受sms状态 * * @Title: onResult */ voID onResult(String smsContent); }}
2.使用:
SmsObserver smsObserver = new SmsObserver(this,new Handler(),new SmsObserver.SmsListener() { @OverrIDe public voID onResult(String smsContent) { //根据需求写 //如:editText.setText(smsContent); } });this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"),true,smsObserver);
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android 短信验证码自动填写全部内容,希望文章能够帮你解决Android 短信验证码自动填写所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)