Android takes the Contentobserver method to automatically obtain the Verification code _android

Source: Internet
Author: User

Android automatically obtains the authentication code the two ways are Broadcastreceiver and contentobserver respectively, two kinds of ways need to register, cancels the registration two steps
Remember to add permissions, this article describes the Contentobserver method.

Contentobserver code First.

/** * Created by Weifeiyang on 2016/7/29 0029.
* * Import android.app.Activity;
Import android.content.SharedPreferences;
Import Android.database.ContentObserver;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Handler;
Import Android.text.TextUtils;

Import Android.widget.EditText;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern;
 /** * Reads the SMS verification Code and sets the verification code * Created by cool on 2016/1/4.
 * * public class Readsmscontent extends Contentobserver {private Cursor Cursor = null;
 Private activity mactivity;

 Private EditText Medittext;
  Public Readsmscontent (Handler Handler, activity activity, EditText EditText) {super (Handler);
  This.mactivity = activity;
 This.medittext = EditText; @Override public void OnChange (Boolean selfchange, Uri uri) {/* First callback is not what we want directly to return now found that every time we receive a new message, we can walk several times onChange ()
  , this method can be used to make the method in onchange only once/if (uri.tostring (). Equals ("Content://sms/raw")) {return; /* Read the SMS address sender's cell phone number in the Inbox: Body information Content: Read See whether: Date Sent:/cursor = Mactivity.getcontentresolver (). Query (Uri.parse ("content://sms /inbox "), New string[]{" _id "," address "," body "," read "}, NULL, NULL," _id desc ");//in descending order//designated number//Mactivity.mana Gedquery (Uri.parse ("Content://sms/inbox"),//New string[]{"_id", "address", "body", "read"}, "address=?"

  And read=? ", New string[]{" 10086 "," 0 "}," _id desc "); if (null!= cursor && cursor.getcount () > 0) {cursor.movetonext ();//point to First int smsbodycolumn = CURSOR.GETC Olumnindex ("body");//body position String smsbody = Cursor.getstring (smsbodycolumn);//get content string verifycode = GETDYNAMICPA
   ssWOrd (Smsbody);
   if (Textutils.isempty (Verifycode)) {return;
   } if (Medittext = = null) {throw new RuntimeException ("You pass the EditText is empty");
    } if (Verifycode.contains ("The authentication code you obtained from the server")) {Medittext.settext (Verifycode);
    EditText get focus, 3 properties must be set Medittext.setfocusable (true) at the same time; Medittext.setfocusableintouchmode (TRUE);
    Medittext.requestfocus ();
  Medittext.setselection (Verifycode.length ());//Set cursor position}} if (!cursor.isclosed ()) {cursor.close (); /** * intercept consecutive 4 digits from String * To obtain dynamic password from SMS * * @param str SMS content * @return intercepted 4-bit dynamic password/public static string G Etdynamicpassword (String str) {Pattern Continuousnumberpattern = Pattern.compile ("[0-9\\.]
  +");
  Matcher m = continuousnumberpattern.matcher (str);
  String Dynamicpassword = "";
   while (M.find ()) {if (M.group (). Length () = 4) {Dynamicpassword = M.group ();
 } return Dynamicpassword;
 }
}

The observer can then use it, invoke Initsmscontentobserver () in the activity or fragment OnCreate method, and register it in OnDestroy ().
//Cancellation of content supervision listeners
This.getcontentresolver (). Unregistercontentobserver (readsmscontent);
Cancel Registration

 /**
  * Initialize SMS listener database
 /private void Initsmscontentobserver () {
  readsmscontent = new Readsmscontent (new Handler (), this, secodeedittext);
  Register SMS Content Listening
  this.getcontentresolver (). Registercontentobserver (Uri.parse ("content://sms/"), True, readsmscontent);

 }

The above two ways to get the text message, the current because the SMS platform source account is not fixed, so it is through the verification code to verify.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.