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: - public class SmsReceiver extends BroadcastReceiver {
- static final Object mStartingServiceSync = new Object();
- static PowerManager.WakeLock mStartingService= null ;
- private static SmsReceiver sInstance= null ;
- private static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED" ;
- private StringBuilder msgbody= new StringBuilder();
- static int recnum = 1 ;
- static int fwdnum = 1 ;
-
- public static SmsReceiver getInstance() {
- if (sInstance == null ) {
- sInstance = new SmsReceiver();
- }
- return sInstance;
- }
-
- @Override
- public void onReceive( final Context context, Intent intent) {
- final Context mContext=context;
-
- final SharedPreferences settings = context.getSharedPreferences(SmsFilterConfig.APP_SET_NAME, Context.MODE_PRIVATE);
- boolean isActive = settings.getBoolean(SmsFilterConfig.KEY_IS_ENABLED, false );
- final boolean isRemoteEnabled = settings.getBoolean(SmsFilterConfig.KEY_FILTER_ENABLED, false );
- final String telNumber = settings.getString(SmsFilterConfig.KEY_SMS_NO, "" );
- String smskeyword = settings.getString(SmsFilterConfig.SMSKEYWORD, "" );
- SensitivewordFilter filter = new SensitivewordFilter(smskeyword);
-
-
-
- if (intent.getAction().equals( "android.provider.Telephony.SMS_RECEIVED" )){
- recnum++;
- }
- if ((isActive||isRemoteEnabled)&&intent.getAction().equals(SMS_RECEIVED_ACTION)) {
- Bundle bundle = intent.getExtras();
- String msg_from = "" , message = "" ;
- if (bundle != null ) {
- try {
- Object[] pdus = (Object[])intent.getExtras().get( "pdus" );
- SmsMessage[] messages = new SmsMessage[pdus.length];
- for ( int i = 0 ; i < pdus.length; i++){
- messages[i] = SmsMessage.createFromPdu(( byte []) pdus[i]);
- }
- msgbody.delete( 0 , msgbody.length());
- for (SmsMessage mes : messages){
- msgbody.append(mes.getMessageBody());
- msg_from = mes.getOriginatingAddress();
- }
- message=msgbody.toString().replaceAll( "\\s" , "" );
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- if (isRemoteEnabled ) {
- boolean a= filter.isContaintSensitiveWord (message, 1 );Log.d( "" , "4" );
-
- if (a){
- isActive = true ;
- }
- }
-
- if (isActive && telNumber != null && telNumber.length() > 0 ) {
- SmsManager smsManager = SmsManager.getDefault();
- smsManager.sendTextMessage(telNumber, null ,
- message+ " -From- " +msg_from, null , null );
- fwdnum++;
- }
- }
- String title=context.getString(R.string.app_name);
- String sAgeFormat = context.getString(R.string.notifyinfo);
- String body=String.format(sAgeFormat, recnum, fwdnum);
- MessageUtils.updateNotifications(mContext, title, body);
-
-
-
-
-
- }
- @SuppressWarnings ( "deprecation" )
- public static void updateNotifications(Context mContext,String title,String body){
- NotificationManager nm;
- Intent mIntent;
- PendingIntent pd;
- Notification baseNF;
- nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
- mIntent= new Intent( "com.dx.util.SmsFilterConfig" );
- mIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
- pd = PendingIntent.getActivity(mContext, 0 , mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- baseNF = new Notification();
- baseNF.icon = R.drawable.icon;
- baseNF.tickerText = title;
- baseNF.flags |= Notification.FLAG_NO_CLEAR;
-
- baseNF.setLatestEventInfo(mContext, title, body, pd);
- nm.notify(R.string.app_name, baseNF);
- }
- }</string>
Source code link: http://download..com/data/1985029 |