SMS研究(转)记录地址

拦截短信 作者:bingzhi更新于 07月26日访问(70)评论(1) 一、场景需求 在做一些需求的时候碰到了,需要把收费短信给屏蔽了,或者说拦截比较正确。 二、网上查了一些资料 How to prevent SMS going to inbox in Android? http://stackoverflow.com/questions/9175969/how-to-prevent-sms-going-to-inbox-in-android?lq=1 This shows a sample of how to delete SMS https://gist.github.com/vivdub/5178e798d9a00cac4ddb How to delete an SMS from the inbox in Android programmatically? http://stackoverflow.com/questions/419184/how-to-delete-an-sms-from-the-inbox-in-android-programmatically?lq=1 Can we delete an SMS in Android before it reaches the inbox? http://stackoverflow.com/questions/1741628/can-we-delete-an-sms-in-android-before-it-reaches-the-inbox?lq=1 Android SMS intercept without notification icon or WAP-PUSH messages http://stackoverflow.com/questions/1732537/android-sms-intercept-without-notification-icon-or-wap-push-messages 三、大致整理如下: 1、需要拦截短信用一个广播接收器去接收接收短信的广播 2、这个广播接收器的优先级要比别人都高 四、主要代码如下 1、SMSReceiver 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public class SMSReceiver extends BroadcastReceiver { private static final String TAG = "SMSReceiver "; public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; private static final String MSG_KEY1 = "key1"; private static final String MSG_KEY2 = "key2"; public SMSReceiver() { super(); Log.i(TAG, "SMSReceiver()"); } @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "onReceive()"); Log.i("onReceive", ""+intent.getAction()); if(SMS_RECEIVED.equals(intent.getAction())){ Object[] pdus = (Object[]) intent.getExtras().get("pdus");//获取短信内容 for(Object pdu:pdus){ byte[] data = (byte[])pdu;//获取单条短信内容,短信内容以pdu格式存在 SmsMessage message = SmsMessage.createFromPdu(data);//使用pdu格式的短信数据生成短息对象 String sender = message.getOriginatingAddress();//获取短信的发送者 String content = message.getMessageBody();//获取短信的内容 Date date = new Date(message.getTimestampMillis()); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sendTime = format.format(date); Log.i(TAG, "sender:"+sender+" content:"+content+" sendTime:"+sendTime); //SmsManager manager = SmsManager.getDefault(); //把你拦截到的短信发送到指定号码,此处为5566 //manager.sendTextMessage("5566", null, "发送人:"+sender+"----发送时间:"+sendTime+"----内容:"+content, null, null); //if("number".equals(sender)){ // abortBroadcast(); //} //如果不想让主机接收某个号码的信息、number为指定号码 Log.i("content.contains"+MSG_KEY1, ""+(content.contains(MSG_KEY1))); Log.i("content.contains"+MSG_KEY2, ""+(content.contains(MSG_KEY2))); //如果短信内容包含MSG_KEY1则拦截该短信 //或者可以其他条件eg:号码是什么的时候拦截该短信 if(content.contains(MSG_KEY1)){ //广播不在往下传 abortBroadcast(); // }else if(content.contains(MSG_KEY2)){ abortBroadcast(); } } } } } 2、在AndroidMenifest.xml中配置 1 2 3 4 5 6 7 8

<!-- 2147483647为Integer.MAX_VALUE -->

3、在使用的Activity中OnCreate()中注册 1 2 3 4 smsReceiver = new SMSReceiver(); IntentFilter filter = new IntentFilter(); filter.setPriority(Integer.MAX_VALUE); registerReceiver(smsReceiver, filter); 4、在使用的Activity中的OnDestroy中 1 unRegistSmsReceiver(); 1 2 3 4 5 private void unRegistSmsReceiver(){ if(smsReceiver!=null){ unregisterReceiver(smsReceiver); } } 五、By the Way 下面代码可以查看这些短信广播接收者的优先级 1 2 3 4 5 Intent smsReceIntent = new Intent("android.provider.Telephony.SMS_RECEIVED"); List infos = this.getPackageManager().queryBroadcastReceivers(smsReceIntent, 0); for(ResolveInfo info:infos){ Log.i(TAG, ""+("Receiver: "+info.activityInfo.name+", priority:"+info.priority)); } 声明:eoe文章著作权属于作者,受法律保护,转载时请务必以超链接形式附带如下信息 原文作者: bingzhi

原文地址: http://my.eoe.cn/787339/archive/5967.html

猜你喜欢

转载自zyzzsky.iteye.com/blog/1944922