Two implementations of Android intercept verification code

Source: Internet
Author: User

In the verification of mobile phone verification code, in order to improve the user experience, the implementation of automatic interception verification filled line, the implementation of this function has two methods, respectively, using the Android broadcast mechanism and Android Contentobserver implementation.

The first way to do this is as follows:

/**
* Listen to the verification code information returned, and automatically supplement such as verification code in the input box [first method]
*/
Public Broadcastreceiver getmessagereceive = new Broadcastreceiver () {
String address;

@Override
public void OnReceive (context context, Intent Intent) {
LOG.V (tag+ "--broadcastreceiver--", "Enter OnReceive");
String msgcode = null;
if (Intent.getaction (). Equals (
"Android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = Intent.getextras ();
if (bundle! = null) {
Object[] PDUs = (object[]) Intent.getextras (). Get ("PDUs");
for (Object Pdu:pdus) {
Smsmessage message = Smsmessage
. CREATEFROMPDU ((byte[]) PDU);
Get text message body content
Content = Message.getmessagebody (). Trim ();
Get the sender of a text message
Address = message.getoriginatingaddress ();
}
LOG.V (tag+ "Broadcastreceiver address", "phone number = ="
+ address);
if (address! = null && address.equals ("10658464")) {
String regex = "\\d*";
Pattern p = pattern.compile (regex);
Matcher m = p.matcher (content);
while (M.find ()) {
if (M.group (). Length () = = 6) {
Msgcode = M.group ();
}
}
LOG.V (tag+ "Broadcastreceiver Msgcode",
"Verification Code = =" + Msgcode);
Metverify.settext (Msgcode);
}
}
}
}

};

In the OnCreate method of activity, you need to register this broadcast:

Filter = new Intentfilter ("Android.provider.Telephony.SMS_RECEIVED");
Getapplicationcontext (). Registerreceiver (getmessagereceive, filter);

In this way, after receiving the message, the broadcast will be sent, and then intercepted, but this method after android4.4, some phones will not be able to intercept normally, so the second method is recommended.

The second method is implemented as follows:

/**
* Monitor SMS Database
*/
Class Smscontent extends Contentobserver {

private cursor cursor = NULL;

Public smscontent (Handler Handler) {
Super (handler);
}

@Override
public void OnChange (Boolean selfchange) {

Super.onchange (Selfchange);
Read the SMS for the specified number in the Inbox
cursor = Managedquery (Uri.parse ("Content://sms/inbox"), New string[]{"_id", "Address", "read", "Body"},
"Address=?" And read=? ", New string[]{" 10658464 "," 0 "}," _id desc ");//Sort by ID, if you sort by date, the text you read will not be allowed after you modify the phone time
if (cursor! = NULL && cursor.getcount () > 0) {
Contentvalues values = new Contentvalues ();
Values.put ("read", "1"); Modify SMS to read mode
Cursor.movetonext ();
int smsbodycolumn = Cursor.getcolumnindex ("Body");
String smsbody = cursor.getstring (Smsbodycolumn);
Ed_mmscode.settext (Getdynamicpassword (smsbody));

}

You cannot invoke the close () method when using Managedquery, otherwise crashes on Android 4.0+ system
if (Build.VERSION.SDK_INT < 14) {
Cursor.close ();
}
}
}

The type of content viewer, after receiving a message, changes, triggering the OnChange method. This method can be intercepted on the test cell phone, it is recommended that you use this method, with demo download: Https://github.com/maliankun/intercept/archive/master.zip

Two implementations of Android intercept verification code

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.