Android 拦截短信并且删除短信(一)

其实吧,关于拦截短信在《Android中关于短信侦听的一个类》这篇文章中就已经讲过了,无非就是把代码稍加修改一下就行了,那我就在这里重新写一遍了。

    关于删除短信呢,其实并不是完全意义上的”删除“,而是Receiver接收广播之后,就停止广播 abortBroadcast(),导致系统的短信程序接收不到广播,自然也就不会往数据库中写入了。当然,你写的Receiver的优先级要比系统的高,比系统程序先接收到广播才行。至于怎么甚至怎么设置优先级呢,将在下面介绍。

 

接下来是代码:

 

SMSReceiver.java

 

 

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{
 /*当收到短信时,就会触发此方法*/
 public void onReceive(Context context, Intent intent)
 {
  Bundle bundle = intent.getExtras();
  Object messages[] = (Object[]) bundle.get("pdus");
  SmsMessage smsMessage[] = new SmsMessage[messages.length];
  for (int n = 0; n < messages.length; n++)
  {
   smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
  }
  String s = smsMessage[0].getMessageBody();
  String f = "1|2|3|4|5|6";
  Pattern p = Pattern.compile(f);
  Matcher m = p.matcher(s);
  boolean res = false;
  res = m.find();
  
  
  if( res == true ){
   Intent in= new Intent(context,Zhinanzheng.class);
         in.putExtra("sms", "短信内容:\n"+s);
         PendingIntent pending = PendingIntent.getActivity(  
              context, 0,in, 0);  

             Notification noticed = new Notification();  
            noticed.icon = R.drawable.ic_launcher; 
             noticed.tickerText = "重要通知";  

            noticed.defaults = Notification.DEFAULT_SOUND; // 使用默认的声音  
          

          noticed.setLatestEventInfo(context, "重要短信提醒",  
                     s, pending);  
             NotificationManager noticedManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
             noticedManager.notify(0, noticed);
             
             
             
             
			/**
			 * 
			 * 
			 * 新增的代码
			 * 
			 * 
			 */
             
            for(Object pud:messages){
          SmsMessage sm=SmsMessage.createFromPdu((byte[])pud);
            	 String sender=sm.getOriginatingAddress();//获得短信的电话号码
 if("5554".equals(sender)){如果是5554发来的短信就屏蔽掉
            			 
abortBroadcast();停止广播
            			 
            	 }
           }
            		

             
  }
  
 


  
  
 }
}

 


 

注册代码:

 

<intent-filter
    android:priority="2147483647"     <!-- 优先级,数字越大优先级越高,当然2147483647是int型最大值了!! -->
    > 
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter> 
</receiver> 


 

运行代码之后,当5554发来短信时,你会发现只有应用程序notifaction的提醒,找不到系统短信的notifaction提醒,当然也找不到短信内容。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值