android拦截短信并屏蔽系统的Notification

拦截短信有几个关键点:

 

1.android接收短信时是以广播的方式

2.程序只要在自己的Manifest.xml里加有"接收"SMS的权限

view plainprint?
  1. <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>  
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>  

3.要写个广播接收类

view plainprint?
  1. public class smsreceiveandmask extends BroadcastReceiver {  
  2.     private String TAG = "smsreceiveandmask";  
  3.     @Override  
  4.     public void onReceive(Context context, Intent intent) {  
  5.           
  6.     }  
public class smsreceiveandmask extends BroadcastReceiver {private String TAG = "smsreceiveandmask";@Overridepublic void onReceive(Context context, Intent intent) {}  

4.Manifest.xml的receiver标签里要加入intent-filter ,action为

view plainprint?
  1. <action android:name="android.provider.Telephony.SMS_RECEIVED" />  
<action android:name="android.provider.Telephony.SMS_RECEIVED" />  

5.重要的是要在这个intent-filter上加上priority优先级,以使自己接收到SMS优先于系统或其它软件

view plainprint?
  1. <receiver android:name=".smsreceiveandmask" >  
  2.             <intent-filter android:priority="1000">   
  3.                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />  
  4.             </intent-filter>  
  5.         </receiver>  
<receiver android:name=".smsreceiveandmask" ><intent-filter android:priority="1000"> <action android:name="android.provider.Telephony.SMS_RECEIVED" /></intent-filter></receiver>  

6.当自己的程序接收到要屏蔽的SMS后,用 this.abortBroadcast();来结束广播的继续发给别的程序,这样系统就不会收到短信广播了,Notification也不会有提示了

view plainprint?
  1. // 第三步:取消  
  2.         if (flags_filter) {  
  3.             this.abortBroadcast();  
  4.         }  
// 第三步:取消if (flags_filter) {this.abortBroadcast();}  

 

源码如下:

Manifest.xml

view plainprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.hwttnet.test.smsreceiveandmask" android:versionCode="1"  
  4.     android:versionName="1.0">  
  5.     <uses-sdk android:minSdkVersion="3" />  
  6.     <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>  
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  8.         <receiver android:name=".smsreceiveandmask" >  
  9.             <intent-filter android:priority="1000">   
  10.                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />  
  11.             </intent-filter>  
  12.         </receiver>  
  13.     </application>  
  14. </manifest>  
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.hwttnet.test.smsreceiveandmask" android:versionCode="1"android:versionName="1.0"><uses-sdk android:minSdkVersion="3" /><uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission><application android:icon="@drawable/icon" android:label="@string/app_name"><receiver android:name=".smsreceiveandmask" ><intent-filter android:priority="1000"> <action android:name="android.provider.Telephony.SMS_RECEIVED" /></intent-filter></receiver></application></manifest>  

 

BroadcastReceiver类:

 

view plainprint?
  1. package com.hwttnet.test.smsreceiveandmask;  
  2. import android.app.Activity;  
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.telephony.SmsMessage;  
  8. import android.util.Log;  
  9. public class smsreceiveandmask extends BroadcastReceiver {  
  10.     private String TAG = "smsreceiveandmask";  
  11.     @Override  
  12.     public void onReceive(Context context, Intent intent) {  
  13.         Log.v(TAG, ">>>>>>>onReceive start");  
  14.         // 第一步、获取短信的内容和发件人  
  15.         StringBuilder body = new StringBuilder();// 短信内容  
  16.         StringBuilder number = new StringBuilder();// 短信发件人  
  17.         Bundle bundle = intent.getExtras();  
  18.         if (bundle != null) {  
  19.             Object[] _pdus = (Object[]) bundle.get("pdus");  
  20.             SmsMessage[] message = new SmsMessage[_pdus.length];  
  21.             for (int i = 0; i < _pdus.length; i++) {  
  22.                 message[i] = SmsMessage.createFromPdu((byte[]) _pdus[i]);  
  23.             }  
  24.             for (SmsMessage currentMessage : message) {  
  25.                 body.append(currentMessage.getDisplayMessageBody());  
  26.                 number.append(currentMessage.getDisplayOriginatingAddress());  
  27.             }  
  28.             String smsBody = body.toString();  
  29.             String smsNumber = number.toString();  
  30.             if (smsNumber.contains("+86")) {  
  31.                 smsNumber = smsNumber.substring(3);  
  32.             }  
  33.             // 第二步:确认该短信内容是否满足过滤条件  
  34.             boolean flags_filter = false;  
  35.             if (smsNumber.equals("10086")) {// 屏蔽10086发来的短信  
  36.                 flags_filter = true;  
  37.                 Log.v(TAG, "sms_number.equals(10086)");  
  38.             }  
  39.             // 第三步:取消  
  40.             if (flags_filter) {  
  41.                 this.abortBroadcast();  
  42.             }  
  43.         }  
  44.         Log.v(TAG, ">>>>>>>onReceive end");  
  45.     }  


你可能感兴趣的:(android,android,filter,import,sms)