android自动接收并填充短信验证码
生活随笔
收集整理的這篇文章主要介紹了
android自动接收并填充短信验证码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這只是部分代碼。大家直接拷貝相應代碼到自己的activity里對應位置即可。
public EditText re_check; private BroadcastReceiver smsReceiver; private IntentFilter filter2; private String strContent; private String patternCoder = "(?<!\\d)\\d{6}(?!\\d)";//匹配六位數字的正則表達式 private Handler handler2 = new Handler() {public void handleMessage(android.os.Message msg) {re_check.setText(strContent);};};@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.act_register);re_check = (EditText) findViewById(R.id.re_regcode);init();}private void init() {filter2 = new IntentFilter();filter2.addAction("android.provider.Telephony.SMS_RECEIVED");filter2.setPriority(Integer.MAX_VALUE);smsReceiver = new BroadcastReceiver() {@Overridepublic 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();if (!TextUtils.isEmpty(from)) {String code = patternCode(message);if (!TextUtils.isEmpty(code)) {strContent = code;handler2.sendEmptyMessage(1);}}}} };registerReceiver(smsReceiver, filter2); }/*** 匹配短信中間的6個數字(驗證碼等)* * @param patternContent* @return*/private String patternCode(String patternContent) {if (TextUtils.isEmpty(patternContent)) {return null;}Pattern p = Pattern.compile(patternCoder);Matcher matcher = p.matcher(patternContent);if (matcher.find()) {return matcher.group();}return null;}@Overrideprotected void onDestroy() {super.onDestroy();unregisterReceiver(smsReceiver);}
?
?
?
總結
以上是生活随笔為你收集整理的android自动接收并填充短信验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: keras实现回归预测
- 下一篇: 王者荣耀手风琴