Android realizes automatic intercept reading function of SMS Verification code _android

Source: Internet
Author: User

Knowledge Preparation:
1. Observer mode of understanding "The article came later ~ ~"
2.Android's cursor uses "Android Basics"
3. Regular expressions use "Java Basics"
4.Handler use "Android Basics"

Code collation:

Mainactivity.java

Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.Menu;
Import Android.view.MenuItem;

Import Android.widget.EditText; public class Mainactivity extends appcompatactivity {public static final int msg_receiver_code = 1; private EditText SMS
Et;
      Private Handler Handler = new Handler () {@Override public void Handlemessage (msg) {switch (msg.what) {
        Case msg_receiver_code:string message= (String) msg.obj;
        Smset.settext (message);
        Smset.setselection (Message.length ());
      Break
    Default:break;
}
  }
};

Private Mysmsobserver Mysmsobserver;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main);
  Smset = (edittext) Findviewbyid (R.id.smset);
  Mysmsobserver = new Mysmsobserver (this, handler); Uri Uri=uri.parse ("COntent://sms ");
Getcontentresolver (). Registercontentobserver (Uri,true, mysmsobserver);
  @Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.menu_main, menu);
return true;
  } @Override protected void OnDestroy () {Super.ondestroy ();
Getcontentresolver (). Unregistercontentobserver (Mysmsobserver);

  @Override public boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid ();
  if (id = = r.id.action_settings) {return true;
return super.onoptionsitemselected (item);

 }
}

 mysmsobserver.java

Import Android.content.Context;
Import Android.database.ContentObserver;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Handler;

Import Android.util.Log;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern;
/** * Created by Nate on 2015/9/28.


* * public class Mysmsobserver extends Contentobserver {private context mcontext; private Handler Mhandler;
  Public Mysmsobserver (context, Handler Handler) {super (Handler);
  This.mcontext = context;
This.mhandler = handler;

  @Override public void OnChange (Boolean selfchange, Uri uri) {super.onchange (Selfchange, URI);
  if (uri.tostring (). Equals ("Content://sms/raw")) {return;
  Uri Queryuri = Uri.parse ("Content://sms/inbox");
  String code = "";
  Cursor Cursor = Mcontext.getcontentresolver (). query (Queryuri, NULL, NULL, NULL, "date desc"); if (cursor!= null) {if (Cursor.movetofirst ()) {String address = cursor.getstring (Cursor.getcolumnindex ("Addre
      SS ")); StRing message = cursor.getstring (Cursor.getcolumnindex ("body")); TODO:2015/9/28 here can do some of their own judgments, such as only a specific mobile phone number to make judgments log.e ("Guxuewu", "address:==>" + address + "message:==&

      gt; "+ message);
      TODO:2015/9/28 Here you can make a specific regular expression of your own project that is programmed to pattern = Pattern.compile ("(\\d{6})");

      Matcher Matcher = pattern.matcher (message);
        if (Matcher.find ()) {code = matcher.group (0);
      Mhandler.obtainmessage (Mainactivity.msg_receiver_code, CODE). Sendtotarget ();
  } cursor.close ();

 }


}
}

Activity_main.xml

<relativelayout 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:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_ Horizontal_margin "
android:paddingright=" @dimen/activity_horizontal_margin "
android:paddingtop=" @dimen /activity_vertical_margin "
tools:context=". Mainactivity ">


<edittext
  android:id=" @+id/smset "
  android:layout_width=" Wrap_content " android:layout_height= "Wrap_content"
  android:layout_centerhorizontal= "true"
  android:ems= "ten"/>

</RelativeLayout>

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.