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

Private domain marketing plan for the medical beauty industry!

In this age where looks matter, appearance anxiet...

Operational framework construction: Make 1 traffic flow more valuable than 4

Because I am good at building frameworks, my arti...

Observation on buying traffic on Tik Tok and Kuaishou platforms!

As the current leaders of short video platforms, ...

Who is the father of radio? The century-long dispute between Tesla and Marconi

Who invented radio communication first? Over the ...

What are the things to pay attention to when developing a mini program?

Mini programs are definitely the most popular pro...

The secret of high-end brands to gain high exposure and high conversion!

There is a famous saying in the traditional adver...

Don't step on the white block game source code

Source code introduction This project is a game s...

Is a face shape that conforms to the golden ratio really "perfect"?

If you want to select the most beautiful face in ...

What do you learn in seo? How do beginners learn SEO?

SEO technology, most people know that companies n...

@Pregnant Mom: Extremely unfriendly! Beware of Toxoplasma gondii in "cats"

Toxoplasma gondii is an intracellular parasite. L...

How to use order-sharing red envelopes to attract new users?

Many people order takeout every day. Before order...

The most complete guide to live streaming sales: KOL placement strategy!

The framework is as follows: 1. What is the real ...