1、申请权限
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
API>23动态申请权限
private static final String[] authBaseArr = {//申请类型Manifest.permission.RECEIVE_SMS,Manifest.permission.READ_SMS,
};
private static final int authBaseRequestCode = 1;
private boolean hasBasePhoneAuth() {PackageManager pm = getPackageManager();for (String auth : authBaseArr) {if (pm.checkPermission(auth, getPackageName()) != PackageManager.PERMISSION_GRANTED) {return false;}}return true;
}private void initNavi() {// 申请权限if (android.os.Build.VERSION.SDK_INT >= 23) {if (!hasBasePhoneAuth()) {this.requestPermissions(authBaseArr, authBaseRequestCode);return;}}
}
2、自定义ContentObserver
/*** 作者:created by meixi* 邮箱:1085220040@qq.com* 日期:2019/12/11 17:37*/
public class SmsObserver extends ContentObserver {private Context mContext;private Handler mHandler;private int mReceivedCode = 1;public SmsObserver(Context context, Handler handler, int received_code) {super(handler);mContext = context;mHandler = handler;mReceivedCode = received_code;}@Overridepublic void onChange(boolean selfChange, Uri uri) {super.onChange(selfChange, uri);String code = "";if (uri.toString().equals("content://sms/raw")) {return;}Uri inboxUri = Uri.parse("content://sms/inbox");final String[] projection = new String[]{"address", "body"};Cursor c = mContext.getContentResolver().query(inboxUri, projection, "read=?", new String[]{"0"}, "date desc");
// Cursor c = mContext.getContentResolver().query(inboxUri, null, null, null, "date desc");if (c != null) {if (c.moveToFirst()) {String address = c.getString(c.getColumnIndex("address"));String body = c.getString(c.getColumnIndex("body"));LgqLogutil.e("发件人为:" + address + "》》》》短信内容为:" + body);Pattern pattern = Pattern.compile("(\\d{4,6})");Matcher matcher = pattern.matcher(body);if (matcher.find()) {code = matcher.group(0);LgqLogutil.e("验证码》》》" + code);ClipboardManager cmb = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);cmb.setText(code);mHandler.obtainMessage(mReceivedCode, code).sendToTarget();}}c.close();}}
}
3、activity实现代码
private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {if (msg.what == 1) {String code = (String) msg.obj;Log.e("lgq","短信===="+code);
// mEtUserVerificationCode.setText(code);//填充}}};
SmsObserver mObserver = new SmsObserver(this, mHandler, 1);Uri uri = Uri.parse("content://sms");
getContentResolver().registerContentObserver(uri, true, mObserver);
附:获取首条短信
String code = "";// Uri inboxUri = Uri.parse("content://sms/inbox");Uri inboxUri = Uri.parse("content://sms");Cursor c = getContentResolver().query(inboxUri, null, null, null, "");if (c != null) {if (c.moveToFirst()) { // if (c.moveToNext()) {String address = c.getString(c.getColumnIndex("address"));String body = c.getString(c.getColumnIndex("body"));LgqLogutil.i("main发件人为:" + address + "》》》》短信内容为:" + body);Pattern pattern = Pattern.compile("(\\d{4,6})");Matcher matcher = pattern.matcher(body);if (matcher.find()) {code = matcher.group(0);LgqLogutil.e("main验证码》》》" + code);ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);cmb.setText(code);mHandler.obtainMessage(mReceivedCode, code).sendToTarget();}}c.close();}