本文实例为大家分享了AndroID实现手机自动获取短信验证码功能,供大家参考,具体内容如下
1、短信监听广播
2、读取短信内容
3、截取短信内容【可以 reg截取】
4、填写至相应控件
PS:DevStore测试没有获取到验证码
源码片段
public class MainActivity extends Activity { private broadcastReceiver smsReceiver; private IntentFilter filter2; private Handler handler; private EditText et; private String strContent; private String patternCoder = "(?<!--\d)\d{6}(?!\d)"; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); et = (EditText) findVIEwByID(R.ID.et); handler = new Handler() { public voID handleMessage(androID.os.Message msg) { et.setText(strContent); }; }; filter2 = new IntentFilter(); filter2.addAction("androID.provIDer.Telephony.SMS_RECEIVED"); filter2.setPriority(Integer.MAX_VALUE); smsReceiver = new broadcastReceiver() { @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(); Log.d("logo","message " + message); // 短息的手机号。。+86开头? String from = sms.getoriginatingAddress(); Log.d("logo","from " + from); Time time = new Time(); time.set(sms.getTimestampMillis()); String time2 = time.format3339(true); Log.d("logo",from + " " + message + " " + time2); strContent = from + " " + message; handler.sendEmptyMessage(1); if (!TextUtils.isEmpty(from)) { String code = patternCode(message); if (!TextUtils.isEmpty(code)) { strContent = code; handler.sendEmptyMessage(1); } } } } }; registerReceiver(smsReceiver,filter2); } @OverrIDe protected voID onDestroy() { super.onDestroy(); unregisterReceiver(smsReceiver); } /** * 匹配短信中间的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; } }</pre-->
文章末尾为大家在分享两个专题,供大家参考:
《PHP验证码大全》
《ASP.NET验证码大全》
以上就是AndroID通过手机自动获取短信验证码的详细代码,希望对大家实现手机自动获取短信验证码有所帮助。
总结以上是内存溢出为你收集整理的Android如何通过手机自动获取短信验证码全部内容,希望文章能够帮你解决Android如何通过手机自动获取短信验证码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)