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

Short video traffic from 0 to 100 million, you only need to learn these 3 steps

At present, the training on traffic improvement i...

On Mother's Day, I wrote copy for Jiang Xiaobai, Durex, and NetEase Cloud Music!

Durex and Jiang Xiaobai took advantage of the pos...

Analysis of Pop Mart’s Private Domain Operations

The blind box craze has made Pop Mart popular, ma...

The operational procedures of event planning scheme are universal!

Preface: This template is a relatively general ac...

Five key points of the new media operation matrix!

The concept of "private domain traffic"...

Foreign media exposes FF: As long as Jia Yueting is there, no car can be built

Foreign media Jalopnik published a long article a...

To do word-of-mouth marketing well, these 5 "T"s are essential

In today's Internet age where everyone is a s...

Sogou advertising, account opening, and display formats

What is Sogou Promotion? Sogou search promotion i...