android 短信验证码自动填写

做过app开发的肯定做过短信验证这块功能,有得app做的比较好的话收到短信会自动帮你填写在输入框中,这样优雅的提供用户体验,

短信毕竟是系统级的应用,但是他给我们提供了接口,使用的是ContentObserver这个类,帮助我们来监听短信数据库发生的变化,

package com.dbei.smsvalidation;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 监听短信
 */
public class SmsObserver extends ContentObserver {

    private Context mContext;
    private Handler mHandler;

    public SmsObserver(Context context, Handler handler) {
        super(handler);
        mContext = context;
        mHandler = handler;
    }
    @SuppressLint("NewApi") @Override
    public void onChange(boolean selfChange, Uri uri) {
        super.onChange(selfChange, uri);
        String code = "";
        if (uri.toString().equals("content://sms/raw")) {//onChange会执行二次,第二次短信才会入库
            return;
        }
        Uri inboxUri = Uri.parse("content://sms/inbox");//短信收件箱url
        Cursor c = mContext.getContentResolver().query(inboxUri, null, null, null, "date desc");
        if (c != null) {
            if (c.moveToFirst()) {
                String body = c.getString(c.getColumnIndex("body"));//获取短信内容
                Pattern pattern = Pattern.compile("(\\d{6})");//正则表达式   连续6位数字
                Matcher matcher = pattern.matcher(body);

                if (matcher.find()) {
                    code = matcher.group(0);
                    mHandler.obtainMessage(MainActivity.MSG_RECEIVED_CODE, code).sendToTarget();
                }
            }
            c.close();
        }
    }
}

MainActivity.class

package com.dbei.smsvalidation;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends Activity {
	    public static final int MSG_RECEIVED_CODE = 1;
	    private EditText et_ValidateCode = null;
	    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;
	                et_ValidateCode.setText(code);
	            }
	        }
	    };
	    @Override
	    protected void onPause() {
	        super.onPause();
	        getContentResolver().unregisterContentObserver(mObserver);
	    }
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	        et_ValidateCode = (EditText)findViewById(R.id.et_validateCode);
	        mObserver = new SmsObserver(MainActivity.this, mHandler);
	        Uri uri = Uri.parse("content://sms");
	        getContentResolver().registerContentObserver(uri, true, mObserver);
	    }
}


你可能感兴趣的:(android 短信验证码自动填写)