安卓簡訊過濾器小程式,安卓過濾器小程式
對於安卓的簡訊廣播接受者支援,Google應該在安卓4.2以後就開始弱化了,也就是配置起來較麻煩唯一,但是到了5.0的時候就應該完全不支援了。因為Google認為這種技術對使用者個人隱私造成很大影響,事實上也正是如此,駭客可以很容易的擷取到使用者的簡訊。
下面寫一個簡訊過濾的小demo。
/**建立一個簡訊接收器,繼承廣播接受者*/public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { System.out.println("簡訊到來了。 。。。"); Object[] objs = (Object[]) intent.getExtras().get("pdus"); for (Object obj : objs) { // 得到簡訊對象 SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj); String body = smsMessage.getMessageBody(); String sender = smsMessage.getOriginatingAddress(); System.out.println("body:" + body); System.out.println("sender:" + sender); // 終止掉當前的廣播。 if ("5556".equals(sender)) { abortBroadcast(); } } }}
需要在在AndroidManifest.xml配置對應的許可權:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.smsreceiver" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.RECEIVE_SMS"/> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.smsreceiver.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.itheima.smsreceiver.SmsReceiver"> <intent-filter android:priority="1000"> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> <!-- 配置廣播接受者 --> <receiver android:name="com.itheima.smsreceiver.OutCallReceiver"> <intent-filter android:priority="1000"> <!-- 配置廣播接收者關心的事件是撥出電話 --> <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver> </application></manifest>
然後類比給手機發簡訊,就能攔截簡訊了。
Log資訊: