您好,登錄后才能下訂單哦!
先說一句:MIUI請放棄治療!這里給個傳送門:
MIUI通知類短信權(quán)限的坑
識別短信驗證碼并提取還是挺常見的一個需求。所要解決的問題主要有:
1、如何監(jiān)聽
2、如何提取短信中的驗證碼
3、監(jiān)聽多次調(diào)用問題
直接看下面代碼吧,很明了。需要注意的點有就是onChange會多次調(diào)用。其實打Log就看的出了,收到一條短信會調(diào)用兩次onChange,Log結(jié)果如下:
mUri===content://sms/raw/20 mUri===content://sms/inbox/20
安卓7.0以上系統(tǒng),點擊標記為已讀,也會調(diào)用一次
mUri===content://sms
收到一條短信都是uri后面都會有確定的一個數(shù)字,對應數(shù)據(jù)庫的_id,比如上面的20
public static class SMSCodeObserver extends ContentObserver { private Activity mActivity; private static final String TAG = "SMSCodeObserver"; private SMSCodeListener mSMSCodeListener; private Uri mUri; public void setSMSCodeListener(SMSCodeListener SMSCodeListener) { mSMSCodeListener = SMSCodeListener; } public interface SMSCodeListener { void onResult(String code); } public SMSCodeObserver(Handler handler, Activity activity) { super(handler); mActivity = activity; } public void register() { mActivity.getContentResolver().registerContentObserver( Uri.parse("content://sms/"), true, this); } public void unRegister() { mActivity.getContentResolver().unregisterContentObserver(this); } @Override public void onChange(boolean selfChange, Uri uri) { super.onChange(selfChange, uri); if (uri == null) { mUri = Uri.parse("content://sms/inbox"); } else { mUri = uri; } if (mUri.toString().contains("content://sms/raw") || mUri.toString().equals("content://sms")) { return; } LogUtils.d(TAG, "mUri===" + mUri.toString()); AndPermission.with(mActivity) .permission(Manifest.permission.READ_SMS) .requestCode(100) .callback(this) .rationale(new RationaleListener() { @Override public void showRequestPermissionRationale(int requestCode, Rationale rationale) { AndPermission.rationaleDialog(mActivity, rationale) .show(); } }) .start(); } @PermissionYes(100) @SuppressWarnings("unused") private void getPermissionYes(List<String> grantedPermissions) { handleSMS(); } @PermissionNo(100) @SuppressWarnings("unused") private void getPermissionNo(List<String> deniedPermissions) { if (AndPermission.hasPermission(mActivity, Manifest.permission.READ_SMS)) { handleSMS(); } else { AndPermission.defaultSettingDialog(mActivity) .show(); } } private void handleSMS() { /* 按照日期倒序排序 */ Cursor cursor = mActivity.getContentResolver().query(mUri, null, null, null, "date desc"); if (cursor != null) { if (cursor.moveToFirst()) {//游標移動到first位置 /* 發(fā)件人的號碼 */ String address = cursor.getString(cursor.getColumnIndex("address")); /* 短信內(nèi)容 */ String body = cursor.getString(cursor.getColumnIndex("body")); LogUtils.d(TAG, "address:" + address + ",body:" + body); if (!body.contains("驗證碼")) { return; } /* 利用正則提取驗證碼(根據(jù)實際情況修改) */ String code = getSMSCode(body); if (code != null) { if (mSMSCodeListener != null) { mSMSCodeListener.onResult(code); } LogUtils.d(TAG, "code:" + code); } } cursor.close(); } } private static String getSMSCode(String msg) { /*提取正則表達式,需按需修改*/ Pattern p = Pattern.compile("(?<![0-9])([0-9]{6})(?![0-9])"); Matcher m = p.matcher(msg); if (m.find()) { LogUtils.d(TAG, m.group()); return m.group(0); } return null; } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。