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.