Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android攔截短信並屏蔽Notification

Android攔截短信並屏蔽Notification

編輯:關於android開發

攔截短信有幾個關鍵點:

1.android接收短信時是以廣播的方式

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


view plaincopy to clipboardprint?


<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> 

 


3.要寫個廣播接收類


view plaincopy to clipboardprint?


public
class smsreceiveandmask extends BroadcastReceiver { 
    private String TAG = "smsreceiveandmask"; 
    @Override

    public
void onReceive(Context context, Intent intent) { 

    } 

 


4.Manifest.xml的receiver標簽裡要加入intent-filter ,action為


view plaincopy to clipboardprint?


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

 


5.重要的是要在這個intent-filter上加上priority優先級,以使自己接收到SMS優先於系統或其它軟件


view plaincopy to clipboardprint?


<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也不會有提示了


view plaincopy to clipboardprint?


// 第三步:取消

        if (flags_filter) { 
            this.abortBroadcast(); 
        } 

 


源碼如下:

Manifest.xml


view plaincopy to clipboardprint?


<?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類:


view plaincopy to clipboardprint?


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 = SmsMessage.createFromPdu((byte[]) _pdus); 
            } 
            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"); 
    } 


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved