Android realizes automatic extraction of SMS Verification code function _android

Source: Internet
Author: User
Tags key string

This article explains the Android automatic extraction of SMS Authentication code solutions, shared for everyone to refer to, the specific content as follows

Main functions and advantages
1. After receiving the authentication code text message, automatically extracts the verification code in the message to fill in the corresponding input box
2. Can specify a number, only read with him about the text, to avoid the extraction of the source error
3. The use of regular expressions, can match the various types of authentication code

Module Integration Key steps
Copy the Smscontentobserver class in the SRC package in Auto_getcode_demo to your project SRC package

In the Smscontentobserver:
Modify the contents of a regular expression to match the string you want to get

Private String Patterncoder = "\\d{4}";  Regular expressions

Specify a number to read only new messages related to him.

Private Boolean Checkphone (String phone) {
 //TODO auto-generated method stub  
 String c_phone = "10690032980066"; C8/>if (C_phone.equals (phone)) return
  true;
 return false;
}

Specify a key string to confirm that the message is what we want to extract

Private Boolean checkstring (String message) {
 if (textutils.isempty) is return
  false;
 Pattern p = pattern.compile ("Finalshares"); This key string can be customized
 Matcher Matcher = p.matcher (message);
 if (Matcher.find ()) return
  true;
 return false;
}

In the mainactivity:
Registered content Watcher, used to monitor the movement of mobile phone messages

@Override public
void OnCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Smscontentobserver = new Smscontentobserver (this, mhandler); Create 1 Objects    
 registercontentobservers ();         Register Content Viewer
}
      
private void Registercontentobservers () {
 //table content Viewer, passing the test I found I could only listen to this URI-----> content:/ /sms
 //Listener not to other URIs such as Content://sms/outbox
 uri Smsuri = uri.parse ("content://sms");
 Getcontentresolver (). Registercontentobserver (Smsuri, true,smscontentobserver);

Create a handler to receive the information from the Observer, automatically obtain the corresponding verification code when receiving the specified number text message and fill in the corresponding control

Private Handler Mhandler = new Handler () {public 
      
 void Handlemessage (msg) { 
  switch (msg.what) {case 
   M Sg_outboxcontent:
    String outbox = Smscontentobserver.getstrcontent (); 
    Etsmsoutbox.settext (outbox); 
    break; 
   Default: Break;}} 
;

Add appropriate permissions in Androidmanifest

<uses-permission android:name= "Android.permission.READ_SMS"/>
<uses-permission android:name= " Android.permission.RECEIVE_SMS "/>

Environment related
Development Platform: Android

Language: Java

Development tools: Eclipse

Effect Show

The above is the entire content of this article, I hope to help you learn.

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.