SoFunction
Updated on 2025-03-10

Android implements automatic interception and reading function of SMS verification code

This article shares the specific code for Android SMS verification code to automatically intercept and read, for your reference, the specific content is as follows

Knowledge preparation:

1. Understanding of observer pattern [The article comes later~~]
Cursor uses [Android Basics]
3. Regular expressions use [Java Basics]
Use [Android Basics]

Code sorting:

import ;
import ;
import ;
import ;
import .;
import ;
import ;
import ;

public class MainActivity extends AppCompatActivity {

public static final int MSG_RECEIVER_CODE = 1;
private EditText smsEt;
private Handler handler = new Handler() {
 @Override
 public void handleMessage(Message msg) {
 switch () {
  case MSG_RECEIVER_CODE:
  String message= (String) ;
  (message);
  (());
  break;
  default:
  break;
 }
 }
};
private MySmsObserver mySmsObserver;

@Override
protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_main);
 smsEt = (EditText) findViewById();
 mySmsObserver = new MySmsObserver(this, handler);
 Uri uri=("content://sms");
 getContentResolver().registerContentObserver(uri,true, mySmsObserver);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(.menu_main, menu);
 return true;
}


@Override
protected void onDestroy() {
 ();
 getContentResolver().unregisterContentObserver(mySmsObserver);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
 int id = ();

 if (id == .action_settings) {
 return true;
 }

 return (item);
}
}

import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

/**
* Created by Nate on 2015/9/28.
*/
public class MySmsObserver extends ContentObserver {

private Context mContext;
private Handler mHandler;


public MySmsObserver(Context context, Handler handler) {
 super(handler);
  = context;
  = handler;
}

@Override
public void onChange(boolean selfChange, Uri uri) {
 (selfChange, uri);

 if (().equals("content://sms/raw")) {
 return;
 }

 Uri queryUri = ("content://sms/inbox");
 String code = "";
 Cursor cursor = ().query(queryUri, null, null, null, "date desc");
 if (cursor != null) {
 if (()) {
  String address = (("address"));
  String message = (("body"));

  // TODO: 2015/9/28 Here you can make some judgments based on address, such as only a specific mobile phone number can make judgments
  ("guxuewu", "address:==>" + address + " message:==>" + message);

  // TODO: 2015/9/28 Here you can write specific regular expressions according to your own project  Pattern pattern = ("(\\d{6})");
  Matcher matcher = (message);

  if (()) {
  code = (0);
  (MainActivity.MSG_RECEIVER_CODE, code).sendToTarget();
  }
 }

 ();
 }


}
}

activity_main.xml

<RelativeLayout xmlns:andro
xmlns:tools="/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:
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:ems="10" />

</RelativeLayout>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.