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

A look into the 2022 Nobel Prize: How did humans come into being?

At 11:30 a.m. on October 3, 2022, local time in S...

Huawei App Market Brand Resource Bidding Promotion Service Rules!

These rules are based on the Huawei Developer Ser...

Change or die? Will the smart ecosystem platform become the next hot topic?

[[125438]] 2014 was a critical year for domestic ...

Super practical! Let’s talk about icon design process and tips

Icon design requires proficiency in the use of so...

Analysis of the mechanism of TikTok addiction

Nowadays, Tik Tok has become an indispensable for...

How to fall in love with work again after returning from the New Year?

When you return to work after the holiday, Still ...

How to plan a new media marketing promotion plan?

What role does new media marketing play? How to s...