Automatically fill in the SMS verification code received

Automatically fill in the SMS verification code received

Source code introduction

1. SMS monitoring broadcast 2. Read SMS content 3. Intercept SMS content [can be intercepted by reg] 4. Fill in the corresponding control PS: DevStore test did not obtain the verification code source code screenshot

Source code snippet

  1. public   class MainActivity extends Activity {
  2. private BroadcastReceiver smsReceiver;
  3. private IntentFilter filter2;
  4. private Handler handler;
  5. private EditText et;
  6. private String strContent;
  7. private String patternCoder = "(?<!--\\d)\\d{6}(?!\\d)" ;
  8.   
  9. @Override  
  10. protected   void onCreate(Bundle savedInstanceState) {
  11. super .onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. et = (EditText) findViewById(R.id.et);
  14. handler = new Handler() {
  15. public   void handleMessage(android.os.Message msg) {
  16. et.setText(strContent);
  17. };
  18. };
  19. filter2 = new IntentFilter();
  20. filter2.addAction( "android.provider.Telephony.SMS_RECEIVED" );
  21. filter2.setPriority(Integer.MAX_VALUE);
  22. smsReceiver = new BroadcastReceiver() {
  23. @Override  
  24. public   void onReceive(Context context, Intent intent) {
  25. Object[] objs = (Object[]) intent.getExtras().get( "pdus" );
  26. for (Object obj : objs) {
  27. byte [] pdu = ( byte []) obj;
  28. SmsMessage sms = SmsMessage.createFromPdu(pdu);
  29. // Content of the message  
  30. String message = sms.getMessageBody();
  31. Log.d( "logo" , "message " + message);
  32. // The mobile phone number of the text message. . Starting with +86?  
  33. String from = sms.getOriginatingAddress();
  34. Log.d( "logo" , "from " + from);
  35. Time time = new Time();
  36. time.set(sms.getTimestampMillis());
  37. String time2 = time.format3339( true );
  38. Log.d( "logo" , from + " " + message + " " + time2);
  39. strContent = from + " " + message;
  40. handler.sendEmptyMessage( 1 );
  41. if (!TextUtils.isEmpty(from)) {
  42. String code = patternCode(message);
  43. if (!TextUtils.isEmpty(code)) {
  44. strContent = code;
  45. handler.sendEmptyMessage( 1 );
  46. }
  47. }
  48. }
  49. }
  50. };
  51. registerReceiver(smsReceiver, filter2);
  52. }
  53.   
  54. @Override  
  55. protected   void onDestroy() {
  56. super .onDestroy();
  57. unregisterReceiver(smsReceiver);
  58. }
  59.   
  60. /**
  61. * Match the 6 numbers in the middle of the SMS (verification code, etc.)
  62. *
  63. * @param patternContent
  64. * @return
  65. */  
  66. private String patternCode(String patternContent) {
  67. if (TextUtils.isEmpty(patternContent)) {
  68. return   null ;
  69. }
  70. Pattern p = Pattern.compile(patternCoder);
  71. Matcher matcher = p.matcher(patternContent);
  72. if (matcher.find()) {
  73. return matcher.group();
  74. }
  75. return   null ;
  76. }
  77. }</pre-->
  78.                  

Source code link: http://download..com/data/2009981

<<:  Verification code countdown

>>:  XY Apple Assistant: Count down the practical features that will be added to iOS 8.3

Recommend

Apple iOS 13.2 beta adds new feature: can delete Siri dictation history

Apple released the first beta version of iOS 13.2...

Tesla pushes Autopilot update, autopilot mode speed limit 72km/h

According to foreign media reports, Tesla CEO Elo...

iOS 12 has these hidden tricks? It’s worth buying an iPhone now!

It has been a while since iOS 12 was released. Wi...

Should you specialize in one area, or move toward full-stack operations?

The concept of full stack originated from full st...

Tencent opens up the scars of China's sports industry

Tencent has brought bad news to the entire Chines...

Is AI invincible in fake human voice?

One morning in 2014, Val Kilmer woke up and found...

Phoenix American: 2022 Commercial Real Estate Trends Report

Phoenix American released the "2022 Commerci...

How can e-commerce companies operate Douyin private domain well?

With the rise of live streaming platforms and sho...

Thermos cup turns into a "bomb"? You must know these safety hazards →

In daily life Thermos cup is convenient and has h...