Android編程實(shí)現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法。分享給大家供大家參考,具體如下:
攔截短信有幾個(gè)關(guān)鍵點(diǎn):
1.android接收短信時(shí)是以廣播的方式
2.程序只要在自己的Manifest.xml里加有"接收"SMS的權(quán)限
<uses-permission android:name="android.permission.RECEIVE_SMS"> </uses-permission> <uses-permission android:name="android.permission.RECEIVE_SMS"> </uses-permission>
3.要寫個(gè)廣播接收類
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標(biāo)簽里要加入intent-filter ,action為
<action android:name="android.provider.Telephony.SMS_RECEIVED" /> <action android:name="android.provider.Telephony.SMS_RECEIVED" />
5.重要的是要在這個(gè)intent-filter上加上priority優(yōu)先級,以使自己接收到SMS優(yōu)先于系統(tǒng)或其它軟件
<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.當(dāng)自己的程序接收到要屏蔽的SMS后,用 this.abortBroadcast();來結(jié)束廣播的繼續(xù)發(fā)給別的程序,這樣系統(tǒng)就不會收到短信廣播了,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");
// 第一步、獲取短信的內(nèi)容和發(fā)件人
StringBuilder body = new StringBuilder();// 短信內(nèi)容
StringBuilder number = new StringBuilder();// 短信發(fā)件人
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);
}
// 第二步:確認(rèn)該短信內(nèi)容是否滿足過濾條件
boolean flags_filter = false;
if (smsNumber.equals("10086")) {// 屏蔽10086發(fā)來的短信
flags_filter = true;
Log.v(TAG, "sms_number.equals(10086)");
}
// 第三步:取消
if (flags_filter) {
this.abortBroadcast();
}
}
Log.v(TAG, ">>>>>>>onReceive end");
}
}
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- android notification 的總結(jié)分析
- Android界面 NotificationManager使用Bitmap做圖標(biāo)
- Android中通知Notification使用實(shí)例(振動、燈光、聲音)
- Android中通過Notification&NotificationManager實(shí)現(xiàn)消息通知
- android中創(chuàng)建通知欄Notification代碼實(shí)例
- Android開發(fā) -- 狀態(tài)欄通知Notification、NotificationManager詳解
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android中關(guān)于Notification及NotificationManger的詳解
- Android中Notification 提示對話框
- Android自定義Notification添加點(diǎn)擊事件
相關(guān)文章
基于Android week view仿小米和iphone日歷效果
這篇文章主要為大家詳細(xì)介紹了基于Android week view仿小米和iphone日歷效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android實(shí)現(xiàn)省市區(qū)三級聯(lián)動
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)省市區(qū)三級聯(lián)動,Spinner實(shí)現(xiàn)省市區(qū)的三級聯(lián)動,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android應(yīng)用中使用及實(shí)現(xiàn)系統(tǒng)“分享”接口實(shí)例
為了應(yīng)用的推廣、傳播,很多的應(yīng)用中都有“分享”功能,這篇文章主要介紹了Android應(yīng)用中使用及實(shí)現(xiàn)系統(tǒng)“分享”接口實(shí)例,有興趣的可以了解一下。2016-12-12
實(shí)例講解Android應(yīng)用開發(fā)中Fragment生命周期的控制
這篇文章主要介紹了Android應(yīng)用開發(fā)中Fragment生命周期的控制,Fragment依賴于Activity,所以生命周期方面也受Activity的影響,需要的朋友可以參考下2016-02-02
Android中LayoutInflater.inflater()的正確打開方式
這篇文章主要給大家介紹了關(guān)于Android中LayoutInflater.inflater()的正確打開方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
Android開發(fā) OpenGL ES繪制3D 圖形實(shí)例詳解
這篇文章主要介紹了Android開發(fā) OpenGL ES繪制3D 圖形實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-09-09

