Android编程实现拦截短信并屏蔽系统Notification的方法
本文实例讲述了Android编程实现拦截短信并屏蔽系统Notification的方法。分享给大家供大家参考,具体如下:
拦截短信有几个关键点:
1.android接收短信时是以广播的方式
2.程序只要在自己的Manifest.xml里加有"接收"SMS的权限
<uses-permissionandroid:name="android.permission.RECEIVE_SMS"> </uses-permission> <uses-permissionandroid:name="android.permission.RECEIVE_SMS"> </uses-permission>
3.要写个广播接收类
publicclasssmsreceiveandmaskextendsBroadcastReceiver{ privateStringTAG="smsreceiveandmask"; @Override publicvoidonReceive(Contextcontext,Intentintent){ } publicclasssmsreceiveandmaskextendsBroadcastReceiver{ privateStringTAG="smsreceiveandmask"; @OverridepublicvoidonReceive(Contextcontext,Intentintent){}
4.Manifest.xml的receiver标签里要加入intent-filter,action为
<actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/> <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/>
5.重要的是要在这个intent-filter上加上priority优先级,以使自己接收到SMS优先于系统或其它软件
<receiverandroid:name=".smsreceiveandmask"> <intent-filterandroid:priority="1000"> <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> <receiverandroid:name=".smsreceiveandmask"> <intent-filterandroid:priority="1000"> <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>
6.当自己的程序接收到要屏蔽的SMS后,用this.abortBroadcast();来结束广播的继续发给别的程序,这样系统就不会收到短信广播了,Notification也不会有提示了
//第三步:取消 if(flags_filter){ this.abortBroadcast(); } //第三步:取消if(flags_filter){this.abortBroadcast();}
源码如下:
Manifest.xml
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.hwttnet.test.smsreceiveandmask"android:versionCode="1" android:versionName="1.0"> <uses-sdkandroid:minSdkVersion="3"/> <uses-permissionandroid:name="android.permission.RECEIVE_SMS"></uses-permission> <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"> <receiverandroid:name=".smsreceiveandmask"> <intent-filterandroid:priority="1000"> <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> </application> </manifest>
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.hwttnet.test.smsreceiveandmask"android:versionCode="1"android:versionName="1.0"> <uses-sdkandroid:minSdkVersion="3"/> <uses-permissionandroid:name="android.permission.RECEIVE_SMS"> </uses-permission> <applicationandroid:icon="@drawable/icon"android:label="@string/app_name"> <receiverandroid:name=".smsreceiveandmask"> <intent-filterandroid:priority="1000"> <actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> </application> </manifest>
BroadcastReceiver类:
packagecom.hwttnet.test.smsreceiveandmask; importandroid.app.Activity; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.os.Bundle; importandroid.telephony.SmsMessage; importandroid.util.Log; publicclasssmsreceiveandmaskextendsBroadcastReceiver{ privateStringTAG="smsreceiveandmask"; @Override publicvoidonReceive(Contextcontext,Intentintent){ Log.v(TAG,">>>>>>>onReceivestart"); //第一步、获取短信的内容和发件人 StringBuilderbody=newStringBuilder();//短信内容 StringBuildernumber=newStringBuilder();//短信发件人 Bundlebundle=intent.getExtras(); if(bundle!=null){ Object[]_pdus=(Object[])bundle.get("pdus"); SmsMessage[]message=newSmsMessage[_pdus.length]; for(inti=0;i<_pdus.length;i++){ message[i]=SmsMessage.createFromPdu((byte[])_pdus[i]); } for(SmsMessagecurrentMessage:message){ body.append(currentMessage.getDisplayMessageBody()); number.append(currentMessage.getDisplayOriginatingAddress()); } StringsmsBody=body.toString(); StringsmsNumber=number.toString(); if(smsNumber.contains("+86")){ smsNumber=smsNumber.substring(3); } //第二步:确认该短信内容是否满足过滤条件 booleanflags_filter=false; if(smsNumber.equals("10086")){//屏蔽10086发来的短信 flags_filter=true; Log.v(TAG,"sms_number.equals(10086)"); } //第三步:取消 if(flags_filter){ this.abortBroadcast(); } } Log.v(TAG,">>>>>>>onReceiveend"); } }
希望本文所述对大家Android程序设计有所帮助。