服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android编程实现拦截短信并屏蔽系统Notification的方法

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

2021-04-21 17:05傲慢的上校 Android

这篇文章主要介绍了Android编程实现拦截短信并屏蔽系统Notification的方法,较为详细的分析了Android短信与Notification的原理及对应的设置取消技巧,需要的朋友可以参考下

本文实例讲述了Android编程实现拦截短信并屏蔽系统Notification的方法。分享给大家供大家参考,具体如下:

拦截短信有几个关键点:

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

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

?
1
2
3
4
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>

3.要写个广播接收类

?
1
2
3
4
5
6
7
8
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为

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

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

?
1
2
3
4
5
6
7
8
9
10
<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也不会有提示了

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

源码如下:

Manifest.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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类:

?
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
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程序设计有所帮助。

延伸 · 阅读

精彩推荐