Android自動填充短信驗證碼

1、短信廣播接收器

public class SMSBroadcastReceiver extends BroadcastReceiver {

    /**
     * 短信驗證碼內容
     */
    private String strSMSContent;
    /**
     * 驗證碼長度爲4
     */
    private static final int CODE_LEN = 4;

    private SMSInteraction smsInteraction;

    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] objs = (Object[]) intent.getExtras().get("pdus");
        for (Object obj : objs) {
            byte[] pdu = (byte[]) obj;
            SmsMessage sms = SmsMessage.createFromPdu(pdu);
            // 短信的內容
            String message = sms.getMessageBody();
            String from = sms.getOriginatingAddress();
            strSMSContent = from + "   " + message;
            if (!TextUtils.isEmpty(from)) {
                String code = patternCode(message);
                if (!TextUtils.isEmpty(code)) {
                    strSMSContent = code;
                    if (smsInteraction != null)
                    {
                        smsInteraction.setCodeValue(strSMSContent);
                    }
                }
            }
        }
    }

    /**
     * 匹配短信中間的驗證碼
     *
     * @param message
     * @return
     */
    private String patternCode(String message) {
        /* 正則匹配驗證碼 */
        String patternCoder = "(?<!\\d)\\d{" + CODE_LEN + "}(?!\\d)";
        if (TextUtils.isEmpty(message)) {
            return null;
        }
        Pattern p = Pattern.compile(patternCoder);
        Matcher matcher = p.matcher(message);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }


    public void setSMSInteractionListener(SMSInteraction smsInteraction) {
        this.smsInteraction = smsInteraction;
    }
}

2、接口

public interface SMSInteraction {
    void setCodeValue(String content);
}

3、Activity

public class MainActivity extends Activity implements SMSInteraction, View.OnClickListener {

    private EditText etCode;
    private Button btnSkip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        regSMSReceiver();
    }


    /**
     * 初始化
     */
    private void initView() {
        etCode = (EditText) findViewById(R.id.et_code);
        btnSkip = (Button) findViewById(R.id.btn_skip);
        btnSkip.setOnClickListener(this);
    }

    /**
     * 註冊廣播
     */
    private void regSMSReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        filter.setPriority(Integer.MAX_VALUE);
        SMSBroadcastReceiver smsBR = new SMSBroadcastReceiver();
        registerReceiver(smsBR, filter);
        smsBR.setSMSInteractionListener(this);
    }

    /**
     * 更新驗證碼值
     *
     * @param content
     */
    @Override
    public void setCodeValue(String content) {
        if (etCode != null) {
            etCode.setText(content);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_skip:
                Intent i = new Intent(this, Main2Activity.class);
                startActivity(i);
                //finish();
                break;
            default:
                break;
        }
    }
}


Demo下載地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章