Forward SMS to a specified mobile number by keyword

Forward SMS to a specified mobile number by keyword

Source code introduction

Often we need to filter text messages and forward them to a specific phone number to prevent missing important content. This program implements this function. You can set all messages to be forwarded, or only forward the content with set keywords. You can set multiple keywords, separated by spaces or commas.
When forwarding all SMS, keyword settings will be ignored. When keyword mode is turned on, ignore the switch of forwarding all SMS. The reason for developing this is that there are some similar functions on the Internet, but most of them forward to email and generally do not provide the function of forwarding SMS. Some forwarding SMS are paid services, and all SMS will be sent to their servers, causing information security and privacy leakage risks.
Pay attention to the package name and file name, and do not write the word SMS forward, otherwise it will be blocked by the firewall.
Source code running screenshot

Source code snippet:

  1. public   class SmsReceiver extends BroadcastReceiver {
  2. static   final Object mStartingServiceSync = new Object();
  3. static PowerManager.WakeLock mStartingService= null ;
  4. private   static SmsReceiver sInstance= null ;
  5. private   static   final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED" ;
  6. private StringBuilder msgbody= new StringBuilder();
  7. static   int recnum = 1 ;
  8. static   int fwdnum = 1 ;
  9.       
  10. public   static SmsReceiver getInstance() {
  11. if (sInstance == null ) {
  12. sInstance = new SmsReceiver();
  13. }
  14. return sInstance;
  15. }
  16.   
  17. @Override  
  18. public   void onReceive( final Context context, Intent intent) {
  19. final Context mContext=context;
  20.           
  21. final SharedPreferences settings = context.getSharedPreferences(SmsFilterConfig.APP_SET_NAME, Context.MODE_PRIVATE);
  22. boolean isActive = settings.getBoolean(SmsFilterConfig.KEY_IS_ENABLED, false );
  23. final   boolean isRemoteEnabled = settings.getBoolean(SmsFilterConfig.KEY_FILTER_ENABLED, false );
  24. final String telNumber = settings.getString(SmsFilterConfig.KEY_SMS_NO, "" );
  25. String smskeyword = settings.getString(SmsFilterConfig.SMSKEYWORD, "" );
  26. SensitivewordFilter filter = new SensitivewordFilter(smskeyword);
  27.           
  28. //beginStartingService(context, intent);  
  29.           
  30. if (intent.getAction().equals( "android.provider.Telephony.SMS_RECEIVED" )){
  31. recnum++;
  32. }
  33. if ((isActive||isRemoteEnabled)&&intent.getAction().equals(SMS_RECEIVED_ACTION)) {
  34. Bundle bundle = intent.getExtras(); // ---get the SMS message passed in---  
  35. String msg_from = "" , message = "" ;
  36. if (bundle != null ) {
  37. try {
  38. Object[] pdus = (Object[])intent.getExtras().get( "pdus" );
  39. SmsMessage[] messages = new SmsMessage[pdus.length];
  40. for ( int i = 0 ; i < pdus.length; i++){
  41. messages[i] = SmsMessage.createFromPdu(( byte []) pdus[i]);
  42. }
  43. msgbody.delete( 0 , msgbody.length());
  44. for (SmsMessage mes : messages){
  45. msgbody.append(mes.getMessageBody());
  46. msg_from = mes.getOriginatingAddress();
  47. }
  48. message=msgbody.toString().replaceAll( "\\s" , "" );
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. if (isRemoteEnabled ) {
  54. boolean a= filter.isContaintSensitiveWord (message, 1 );Log.d( "" , "4" );
  55. //Set<string> set = filter.getSensitiveWord("aaefggh", 1);Log.d("", "4");  
  56. if (a){
  57. isActive = true ;
  58. }
  59. }
  60.   
  61. if (isActive && telNumber != null && telNumber.length() > 0 ) {
  62. SmsManager smsManager = SmsManager.getDefault();
  63. smsManager.sendTextMessage(telNumber, null ,
  64. message+ " -From- " +msg_from, null , null );
  65. fwdnum++;
  66. }
  67. }
  68. String title=context.getString(R.string.app_name);
  69. String sAgeFormat = context.getString(R.string.notifyinfo);
  70. String body=String.format(sAgeFormat, recnum, fwdnum);
  71. MessageUtils.updateNotifications(mContext, title, body);
  72. //finishBlockSms();  
  73. /*SharedPreferences.Editor editor = settings.edit();
  74. editor.putInt(SmsFilterConfig.KEY_REC_NUM, recnum);
  75. editor.commit();*/  
  76. //MessageUtils.writeIntPref(mContext,SmsFilterConfig.KEY_REC_NUM, recnum);  
  77. // MessageUtils.writeIntPref(mContext,SmsFilterConfig.KEY_FWD_NUM, fwdnum);  
  78. }
  79. @SuppressWarnings ( "deprecation" )
  80. public   static   void updateNotifications(Context mContext,String title,String body){
  81. NotificationManager nm;
  82. Intent mIntent;
  83. PendingIntent pd;
  84. Notification baseNF;
  85. nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
  86. mIntent= new Intent( "com.dx.util.SmsFilterConfig" );
  87. mIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
  88. pd = PendingIntent.getActivity(mContext, 0 , mIntent, PendingIntent.FLAG_UPDATE_CURRENT); //If transferring content, use m_Intent();  
  89. baseNF = new Notification();
  90. baseNF.icon = R.drawable.icon;
  91. baseNF.tickerText = title;
  92. baseNF.flags |= Notification.FLAG_NO_CLEAR;
  93. //Set the parameters for notification display  
  94. baseNF.setLatestEventInfo(mContext, title, body, pd);
  95. nm.notify(R.string.app_name, baseNF);
  96. }
  97. }</string>

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

<<:  Robin Li talks about the difference between Baidu and Google: User-generated content

>>:  Various styles of gesture sliding Cell

Recommend

The mini program is launched amazingly, but why do I think it is bad?

In the early morning of January 9, the WeChat min...

Android 7.0 vs. iOS 10: Which is better?

The competition in the mobile operating system spa...

Five rules to start your growth hacking journey

The Origins of Growth Hacking Originated from Sil...

What exactly is the “Arctic Catfish”?

The "Arctic Catfish" incident has come ...

2019 Internet Marketing Promotion Tips!

With the gradual improvement of current Internet ...

Want to reduce microplastics in water? Boil it before drinking丨Tech Weekly

Compiled by Zhou Shuyi This fish is only the size...

Introduction to Xiaomi news resources and advertising formats

As more and more application markets begin to dev...