Android之——攔截簡訊

來源:互聯網
上載者:User

標籤:提示   .net   odi   rac   get   creat   over   public   manifest   

轉載請註明出處:http://blog.csdn.net/l1028386804/article/details/46994097

這裡。向大家簡介通過BroadcastReceiver來攔截簡訊的方法

1、建立簡訊廣播接收者SmsRecevier

這個類是BroadcastReceiver的子類,詳細的攔截操作在這個類中實現。我在這裡僅僅是簡單的介紹一下方法,把擷取到的簡訊列印資訊出來。

詳細的商務邏輯就要大家自己去實現了。

詳細代碼例如以下:

package com.lyz.receiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.telephony.SmsMessage;import android.util.Log;/** * 簡訊廣播接收者 * @author liuyazhuang * */public class SmsRecevier extends BroadcastReceiver {private static final String TAG = "SmsRecevier";@Overridepublic void onReceive(Context context, Intent intent) {Object[] pdus = (Object[]) intent.getExtras().get("pdus");for(Object pdu:pdus){SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])pdu);String body = smsMessage.getDisplayMessageBody();Log.i(TAG, body);}}} 
2、注冊授權資訊

詳細實現例如以下:

<?

xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lyz.sms" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.RECEIVE_SMS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.lyz.sms.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 配置廣播攔截簡訊 --> <receiver android:name="com.lyz.receiver.SmsRecevier"> <intent-filter android:priority="1000"> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> </application></manifest>

大功告成。是不是和《Android之——攔截撥出電話》一樣簡單呢?
溫馨提示:大家能夠到http://download.csdn.net/detail/l1028386804/8921125攔截擷取完整的代碼示範範例

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.