The author found that in many applications, there are automatic access to the Verification code function: Click to get the Verification Code button, received SMS, the current application does not need to exit the program can get to the text of the verification code, and automatically fill. Feel this kind of user experience is very good, do not need a user to switch back and forth between SMS and app, how is this function realized? In fact, is very simple, is to use a android called Contentobserver things, the following manual simple implementation of a ~ ~ ~
I. Development of Contentobserver
Mainly used to listen to the contents of the Inbox, once a new message came over, to listen to whether it is the number I want to send the message, if it is, directly with regular expression of interception
/** * SMS Verification Code INTERCEPT * * @author yangfan * */public class Smscontentobserver extends Contentobserver {private context Mcontext; Context private Handler Mhandler; Update UI thread private String code;
Authentication Code Public Smscontentobserver (context context, Handler Handler) {super (Handler);
Mcontext = context;
Mhandler = handler;
/** * Callback function, callback this method when the URI being listened to changes * Note that when the message is received, it will be recalled two times * * @param selfchange * This value is not significant in general case this callback value false
* * @Override public void OnChange (Boolean selfchange, Uri uri) {log.e ("xxxxxxxxxxxxxxxx", uri.tostring ());
The first callback is not what we want to return directly if (Uri.tostring (). Equals ("Content://sms/raw")) {returns;
//Second callback query inbox content Uri Inboxuri = Uri.parse ("Content://sms/inbox");
Sort SMS database in chronological order Cursor C = mcontext.getcontentresolver (). query (Inboxuri, NULL, NULL, NULL, "date desc"); if (c!= null) {if (C.movetofirst ()) {//Get cell phone number String address = c.getstring (C.getcolumnindex ("address")
); Get SMS Content String BODY = c.getstring (C.getcolumnindex ("body"));
Determine if the phone number is the target number if (!address.equals ("Your target number")) {return;
The//Regular expression intercepts the 6-bit code pattern = Pattern.compile ("(\\d{6})") in SMS;
Matcher Matcher = Pattern.matcher (body);
If found via handler sent to the main thread if (Matcher.find ()) {code = matcher.group (0);
Mhandler.obtainmessage (1, code). Sendtotarget ();
}} c.close ();
}
}
two, layout, and activity
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android: padding= "50DP" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: text= "Your authentication code is:"/> <edittext android:id= "@+id/smscode" android:focusable= "false" Android:focusableintouchmode
= "false" android:layout_width= "Match_parent" android:layout_height= "wrap_content"/> </LinearLayout>
public class Mainactivity extends actionbaractivity {private EditText code;
@SuppressLint ("Handlerleak") Handler Handler = new Handler () {@Override public void Handlemessage (msg) {
if (msg.what = = 1) {Code.settext (msg.obj.toString ());
}
}
};
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main); CoDe = (edittext) Findviewbyid (R.id.smscode);
Smscontentobserver smscontentobserver = new Smscontentobserver (mainactivity.this, handler); MainActivity.this.getContentResolver (). Registercontentobserver (Uri.parse ("content://sms/"), True,
Smscontentobserver);
}
}
Third, do not forget to add permissions
<uses-permission android:name= "Android.permission.READ_SMS"/>
Iv. Run the test
I'm taking two mobile phones to test ~ ~ ~
Automatically get SMS verification code. PNG
Code Address: Https://github.com/yungfan/SMSSMSContentObserver
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.