本文实例讲述了AndroID编程实现拦截短信并屏蔽系统Notification的方法。分享给大家供大家参考,具体如下:
拦截短信有几个关键点:
1.androID接收短信时是以广播的方式
2.程序只要在自己的Manifest.xml里加有"接收"SMS的权限
<uses-permission androID:name="androID.permission.RECEIVE_SMS"></uses-permission><uses-permission androID:name="androID.permission.RECEIVE_SMS"></uses-permission>
3.要写个广播接收类
public class smsreceiveandmask extends broadcastReceiver { private String TAG = "smsreceiveandmask"; @OverrIDe public voID onReceive(Context context,Intent intent) {}public class smsreceiveandmask extends broadcastReceiver { private String TAG = "smsreceiveandmask"; @OverrIDepublic voID onReceive(Context context,Intent intent) {}
4.Manifest.xml的receiver标签里要加入intent-filter,action为
<action androID:name="androID.provIDer.Telephony.SMS_RECEIVED" /><action androID:name="androID.provIDer.Telephony.SMS_RECEIVED" />
5.重要的是要在这个intent-filter上加上priority优先级,以使自己接收到SMS优先于系统或其它软件
<receiver androID:name=".smsreceiveandmask" > <intent-filter androID:priority="1000"> <action androID:name="androID.provIDer.Telephony.SMS_RECEIVED" /> </intent-filter> </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也不会有提示了
// 第三步:取消 if (flags_filter) { this.abortbroadcast();}// 第三步:取消if (flags_filter) {this.abortbroadcast();}
源码如下:
Manifest.xml
<?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>
<?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类:
package com.hwttnet.test.smsreceiveandmask;import androID.app.Activity;import androID.content.broadcastReceiver;import androID.content.Context;import androID.content.Intent;import androID.os.Bundle;import androID.telephony.SmsMessage;import androID.util.Log;public class smsreceiveandmask extends broadcastReceiver { private String TAG = "smsreceiveandmask"; @OverrIDe public voID onReceive(Context context,Intent intent) { Log.v(TAG,">>>>>>>onReceive start"); // 第一步、获取短信的内容和发件人 StringBuilder body = new StringBuilder();// 短信内容 StringBuilder number = new StringBuilder();// 短信发件人 Bundle bundle = intent.getExtras(); if (bundle != null) { Object[] _pdus = (Object[]) bundle.get("pdus"); SmsMessage[] message = new SmsMessage[_pdus.length]; for (int i = 0; i < _pdus.length; i++) { message[i] = SmsMessage.createFromPdu((byte[]) _pdus[i]); } for (SmsMessage currentMessage : message) { body.append(currentMessage.getdisplayMessageBody()); number.append(currentMessage.getdisplayOriginatingAddress()); } String smsBody = body.toString(); String smsNumber = number.toString(); if (smsNumber.contains("+86")) { smsNumber = smsNumber.substring(3); } // 第二步:确认该短信内容是否满足过滤条件 boolean flags_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,">>>>>>>onReceive end"); }}
希望本文所述对大家AndroID程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android编程实现拦截短信并屏蔽系统Notification的方法全部内容,希望文章能够帮你解决Android编程实现拦截短信并屏蔽系统Notification的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)