Recently, during registration, many apps directly enter the verification code when receiving text messages from their mobile phones. The user's automatic input is omitted on the interface. This is really user-friendly, so I made a new one myself.
Steps:
First, I used ContentObserver to listen for text messages. (You 'd better know the verification code sent from that number)
Then, you can get the verification code from the group that uses regular expressions in the text message (of course, the format of the verification code is required)
Paste the key code:
Register the SMS Database
ContentObserver c = new ContentObserver (han) {@ Overridepublic void onChange (boolean selfChange) {// TODO Auto-generated method stubsuper. onChange (selfChange); han. sendEmptyMessage (0) ;}}; getContentResolver (). registerContentObserver (Uri. parse ("content: // sms"), true, c); Handler han = new Handler () {@ SuppressWarnings ("deprecation") public void handleMessage (android. OS. message msg) {String codestr = null; tr Y {codestr = Cus_UnitTools.getsmsyzm (Reg_ForgetPassword.this); code. setText (codestr); requestcode ();} catch (Exception e) {Log. e ("yung", "Verification code extraction failed:" + codestr) ;};}; public static String getsmsyzm (Activity c) {Uri uri = Uri. parse ("content: // sms/inbox"); String [] projection = new String [] {"address", "person", "body "}; string selection = "address = '" + JTPHONE + "'"; String [] selectionArgs = new String [] {}; String sortOrder = "date desc"; @ SuppressWarnings ("deprecation") Cursor cur = c. managedQuery (uri, projection, selection, selectionArgs, sortOrder); if (cur! = Null & cur. getCount ()> 0) {cur. moveToFirst (); String body = cur. getString (cur. getColumnIndex ("body ")). replaceAll ("\ n", ""); cur. close (); return getyzm (body, YZMLENGTH);} cur. close (); return null ;} /*** extract the verification code from the text message character seek * @ param body text message content * @ param YZMLENGTH the length of the verification code is generally 6 or 4 characters * @ return the obtained Verification Code */public static string getyzm (String body, int YZMLENGTH) {// first ([a-zA-Z0-9] {YZMLENGTH}) is to get a continuous six-digit number //(?
// Some verification codes are pure numbers, so you can use them directly.
// Pattern p = Pattern. compile ("(?
After the listener is complete, remember getContentResolver (). unregisterContentObserver (c); cancel the listener
In this way, you can monitor the changes in the text message database. Remember to add the permission to list the text message permissions by the way.
The demo will not be written. It is very clear and simple here. Don't like it, don't spray it, there is a wrong reply, repost must respect the author-yung7086