Use a broadcast receiver to snoop text messages

Source: Internet
Author: User

If you want to eavesdrop on text messages received by others for your ulterior motives, the content of this section can meet your needs.

When the system receives a text message, it sends an action named android. provider. telephony. sms_received broadcast intent, which stores the received text message content. You can get the text message content from the intent by using the name "PDUS.

Public class incomingsmsreceiver extends broadcastreceiver {
Private Static final string sms_received = "android. provider. telephony. sms_received ";
@ Override public void onreceive (context, intent ){
If (intent. getaction (). Equals (sms_received )){
Smsmanager SMS = smsmanager. getdefault ();
Bundle bundle = intent. getextras ();
If (bundle! = NULL ){

Object [] PDUS = (object []) bundle. Get ("PDUS ");
Smsmessage [] messages = new smsmessage [PDUS. Length];
For (INT I = 0; I <PDUS. length; I ++) messages [I] = smsmessage. createfrompdu (byte []) PDUS [I]);
For (smsmessage message: messages ){
String MSG = message. getmessagebody ();
String to = message. getoriginatingaddress ();
SMS. sendtextmessage (to, null, MSG, null, null );
}}}}}

In the <Application> node of the androidmanifest. xml file, connect to the broadcast intent that receives the SMS for subscription:
<Cycler Android: Name = ". incomingsmsreceiver">
<Intent-filter> <action Android: Name = "android. provider. telephony. sms_received"/> </intent-filter> </receiver>
Add the following permissions to the androidmanifest. xml file:

<Uses-Permission Android: Name = "android. Permission. receive_sms"/> <! -- Receive SMS -->
<Uses-Permission Android: Name = "android. Permission. send_sms"/> <! -- Send SMS permission -->

In addition to the arrival of SMS broadcast intent, Android also has many broadcast intent types, such as 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>

And you need to declare the permission:
<Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>


In Android, the response of a program is monitored by the system services activity manager and window manager. When broadcastreceiver is not executed 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. Instead of using sub-threads, because broadcastreceiver has a short life cycle (the broadcastreceiver instance will be destroyed after onreceive () is executed ), the sub-thread may have ended before it has ended. Of course, if broadcastreceiver ends, its host process is still running, and the sub-thread will continue to execute. However, the host process is easily killed first when the system requires memory because it is a null process (a process without any active components ).

Public class incomingsmsreceiver extends broadcastreceiver {
@ Override public void onreceive (context, intent ){
// Send intent to start the service. The service performs time-consuming operations.
Intent service = new intent (context, xxxservice. Class );
Context. startservice (service );
}
}

Each time a message is broadcast, A broadcastreceiver instance is created and the onreceive () method is executed.

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.