In the registration, real-name authentication, modification of mobile phone number, we will use the function of sending verification code, here will often involve 2 effects,
First: Send a verification code countdown effect;
The second one: Automatically gets the received SMS and fills in the verification code.
First, the countdown effect:
It's really simple, it's a timer. Refresh every second, the countdown is finished. You can re-click to get it.
<pre name= "code" class= "Java" >btn_getyzm.setenabled (false); Btn_getyzm.setbackgroundresource ( R.drawable.daojishi), task = new TimerTask () {@Overridepublic void run () {Runonuithread (new Runnable () {//UI Thread@overr idepublic void Run () {if (time <= 0) {//When countdown is less than 0 o'clock remember to restore the picture, you can click Btn_getyzm.setenabled (true); Btn_ Getyzm.setbackgroundresource (R.drawable.btn_yangzhengma_selector); Btn_getyzm.settextcolor (Color.parseColor ("# 454545 ")); Btn_getyzm.settext (" Get Verification Code "); Task.cancel ();} else {Btn_getyzm.settext (time + "seconds after Retry"); Btn_getyzm.settextcolor (Color.rgb (125, 125, 125));} time--;}});}; Time = 60;timer.schedule (task, 0, 1000);
Second, automatically listen to new text messages, and parse the inside of the Verification code
Package Com.smsyzm;import Java.util.regex.matcher;import Java.util.regex.pattern;import android.app.Activity; Import Android.database.contentobserver;import Android.database.cursor;import Android.net.uri;import Android.os.handler;import android.widget.edittext;/** * Registration, real-name authentication, modify mobile phone number, automatically obtain verification code, and fill in * */public class Getsmscontent Extends Contentobserver {public final String Sms_uri_inbox = "Content://sms/inbox";p rivate activity activity = Null;priva Te string smscontent = "";p rivate EditText verifytext = null;private String sms_address_prnumber = "400888666";//short-send provider PU Blic getsmscontent (activity activity, Handler Handler, EditText verifytext) {super (Handler); this.activity = activity; This.verifytext = Verifytext;} @Overridepublic void OnChange (Boolean selfchange) {Super.onchange (selfchange); cursor cursor = null;//cursor//Read text of the specified number in the Inbox cursor = Activity.managedquery (Uri.parse (Sms_uri_inbox), new string[] {"_id", " Address "," body "," read "},//properties to read" Address=? " And read=? ",//What is the query condition for new string[] {SMs_address_prnumber, "0"},//Query condition Assignment "date desc");//Sort if (cursor! = NULL) {//If SMS is unread mode Cursor.movetofirst (); if ( Cursor.movetofirst ()) {String smsbody = cursor.getstring (Cursor.getcolumnindex ("body")); System.out.println ("smsbody=======================" + smsbody); String regEx = "[^0-9]"; Pattern p = pattern.compile (regEx); Matcher m = P.matcher (Smsbody.tostring ()), smscontent = M.replaceall (""). Trim (). toString (); if (verifytext! = null && Amp Null!=smscontent &&! "". Equals (smscontent)) {verifytext.settext (smscontent); Verifytext.setselection (Smscontent.length ());}}}}
Call method: Register in the Activity's OnCreate method:
getsmscontent content = new Getsmscontent (registerbyphone.this, New Handler (), EDIT_YZM);
Register SMS Change Monitor
This.getcontentresolver (). Registercontentobserver (Uri.parse ("content://sms/"), true, content);
Demo full download link Click to open link
Android Send verification code countdown effect and automatically get verification code and fill in the input box