源码简介
很多时候我们需要对短信进行筛选,将指定短信转发到特定手机号,以防止遗漏重要内容,本程序就是实现这个功能。可以设置全部转发,也可设置只转发设置关键词内容的转发。可设置多个关键词,关键词以空格或者逗号分隔。
转发全部短信时,将忽略关键词设置。打开关键词模式时,忽略全部转发的开关。开发这个的原因在于,网上现在有些类似功能的,但多数是转发到email,一般不提供转发短信功能,而有的转发短信的是付费服务,同时所有短信会被发送到他们的服务器,造成信息安全、隐私泄露隐患。
包名、文件名方面需注意,不要写sms forward字眼,否则会被防火墙阻挡。
源码运行截图
源码片段:
- 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);
- //beginStartingService(context, intent);
- if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
- recnum++;
- }
- if ((isActive||isRemoteEnabled)&&intent.getAction().equals(SMS_RECEIVED_ACTION)) {
- Bundle bundle = intent.getExtras(); // ---get the SMS message passed in---
- 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");
- //Set<string> set = filter.getSensitiveWord("aaefggh", 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);
- //finishBlockSms();
- /*SharedPreferences.Editor editor = settings.edit();
- editor.putInt(SmsFilterConfig.KEY_REC_NUM, recnum);
- editor.commit();*/
- //MessageUtils.writeIntPref(mContext,SmsFilterConfig.KEY_REC_NUM, recnum);
- // MessageUtils.writeIntPref(mContext,SmsFilterConfig.KEY_FWD_NUM, fwdnum);
- }
- @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); //如果转移内容则用m_Intent();
- 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>
源码链接:http://down.51cto.com/data/1985029