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.