Android編程實現攔截簡訊並屏蔽系統Notification的方法_Android

來源:互聯網
上載者:User

本文執行個體講述了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程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.