关闭

Android自动获得短信验证码

标签: android短信验证码
201人阅读 评论(0) 收藏 举报
分类:

Android自动获得短信验证码

1:<!--动态读取手机短信权限-->
<uses-permission android:name="android.permission.READ_SMS" />

2:编写内容观察者类,监听短信触发:
public class SmsObserver extends ContentObserver {
    private Context mContext;
    private Handler mHandler;
    public SmsObserver(Context context, Handler handler) {
        super(handler);
        mContext = context;
        mHandler = handler;
    }
    @Override
    public void onChange(boolean selfChange, Uri uri) {
        super.onChange(selfChange, uri);

        Log.e("DEBUG", "SMS has changed!");
        Log.e("DEBUG", uri.toString());
        String code = "";
        if (uri.toString().equals("content://sms/raw")) {
            return;
        }
        Uri inboxUri = Uri.parse("content://sms/inbox");
        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"));//发件内容
                //即发送短信的号码
               if (!address.equals("10690067200453")) {
                    return;
                }
                Log.e("DEBUG", "发件人为:" + address + " " + "短信内容为:" + body);

                Pattern pattern = Pattern.compile("(\\d{6})");//通过正则表达式获得六位数验证码
                Matcher matcher = pattern.matcher(body);
                if (matcher.find()) {
                    code = matcher.group(0);
                    Log.e("DEBUG", "code is " + code);
                    //发送通过Register.MSG_RECEIVED_CODE发送code到实现类
                    mHandler.obtainMessage(Register.MSG_RECEIVED_CODE, code).sendToTarget();
                }
            }
            c.close();
        }
    }
}

3:实现类:
public class Register extends Activity {

    private EditText codeText;//验证码框
    public static final int MSG_RECEIVED_CODE = 1;

    private SmsObserver mObserver;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == MSG_RECEIVED_CODE) {
                String code = (String)msg.obj;
                codeText.setText(code);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        //动态获取短信验证码
        mObserver = new SmsObserver(Register.this, mHandler);
        Uri uri = Uri.parse("content://sms");
        getContentResolver().registerContentObserver(uri, true, mObserver);//注册短信内容观察者
}
@Override
protected void onPause() {
    super.onPause();
    getContentResolver().unregisterContentObserver(mObserver);//取消内容观察者注册
}

4:图片:
![短信验证码](http://img.blog.csdn.net/20151120222027525)


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:59804次
    • 积分:1201
    • 等级:
    • 排名:千里之外
    • 原创:55篇
    • 转载:12篇
    • 译文:4篇
    • 评论:3条
    最新评论