android 使用廣播接收者監聽簡訊和攔截撥出電話

來源:互聯網
上載者:User

如果你想監聽自己或者別人接收到的簡訊,設定黑名單等功能,那麼就需要以下功能監聽簡訊,並進行提示或者響應的處理:


當系統收到簡訊時,會發出一個廣播Intent,Intent的action名稱為android.provider.Telephony.SMS_RECEIVED,該Intent存放了系統接收到的簡訊內容,我們使用名稱“pdus”即可從Intent中擷取到簡訊內容:

public class IncomingSMSReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

@Override

public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals(SMS_RECEIVED)) {
    SmsManager sms = SmsManager.getDefault(); //得到簡訊管理器
    Bundle bundle = intent.getExtras();                        //得到意圖中的bundle對象
    if (bundle != null) {
    Object[] pdus = (Object[]) bundle.get("pdus");     //得到名稱為"pdus"的對象,是一個Object數組,裡面的每一個元素為一個byte[]數組
    SmsMessage[] messages = new SmsMessage[pdus.length]; 
    for (int i = 0; i < pdus.length; i++) messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
        for (SmsMessage message : messages){
            String msg = message.getMessageBody();    //簡訊內容
            String to = message.getOriginatingAddress(); //簡訊地址
            sms.sendTextMessage(to, null, msg, null, null);

   }

  }

 }

 }

 }

在AndroidManifest.xml檔案中的<application>節點裡對接收到簡訊的廣播Intent進行訂閱:

<receiver android:name=".IncomingSMSReceiver">

     <intent-filter>

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

      </intent-filter>

</receiver>

在AndroidManifest.xml檔案中添加以下許可權:
<uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 接收簡訊許可權 -->
<uses-permission android:name="android.permission.SEND_SMS"/><!-- 傳送簡訊許可權 -->

如果想在播出的電話加上特殊提示或者預設加上區號,12593優惠號等,那麼就需要監聽撥出的電話,並進行處理:

向撥出打電話時系統會發出一個有序廣播,雖然該廣播最終會被拔號器裡的廣播接收者所接收並實現電話拔打,但我們可以在廣播傳遞給拔號廣播接收者之前先得到該廣播,然後清除傳遞給拔號廣播接收者的電話號碼,在拔號廣播接收者接收到該廣播時,由於電話號碼為null,因此取消電話拔打。

public class OutgoingCallReceiver extends BroadcastReceiver {
     public void onReceive(Context context, Intent intent) {
           setResultData(null); //清除電話,廣播被傳給系統的接收者後,因為電話為null,取消電話拔打
      
          // 同樣如果你想修改外拔的電話號碼,可以這樣做
          // String phone = getResultData();//得到外拔電話
          // setResultData(“12593”+ phone);//在電話前面加上12593
     }
}

接收外拔電話廣播Intent,在AndroidManifest.xml檔案中的<application>節點裡訂閱此Intent:
<receiver android:name=".OutgoingCallReceiver">
    <intent-filter android:priority="1">
         <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
    </intent-filter>
</receiver>
並且要進行許可權聲明:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.