Android之——攔截短信
轉載請註明出處: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"; @Override public 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、註冊授權信息
詳細實現例如以下:
<?大功告成。是不是和《Android之——攔截外撥電話》一樣簡單呢?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>
溫馨提示:大家能夠到http://download.csdn.net/detail/l1028386804/8921125攔截獲取完整的代碼演示樣例
Android之——攔截短信