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

Tablet market is a mixed bag: iPad downturn and China's rise

Although Apple's iPad sales have declined for ...

For promotion on Xiaohongshu, just read this article.

This article will try to answer your questions: 1...

Want to lose weight by eating dark chocolate? Wake up!

Audit expert: Wang Guoyi Postdoctoral fellow in N...

The whole process of user growth fission of "old bring new"

Without users, there is no revenue. In an era of ...

ShowGirls' stories: Things you don't know

ShowGirls have always been a beautiful sight at e...