欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程实现拦截短信并屏蔽系统Notification的方法

程序员文章站 2023-12-14 12:38:46
本文实例讲述了android编程实现拦截短信并屏蔽系统notification的方法。分享给大家供大家参考,具体如下: 拦截短信有几个关键点: 1.android接收短...

本文实例讲述了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程序设计有所帮助。

上一篇:

下一篇: