Android listens to SMS messages and android listens to sms messages

Source: Internet
Author: User

Android listens to SMS messages and android listens to sms messages

When the device receives a new SMS message, it broadcasts an Intent containing the android. provider. Telephony. SMS_RECEIVED action. Note that this action is a string value, and SDK 1.0 no longer contains references to this string. Therefore, you need to explicitly specify it in your application.

 

For an application to listen to SMS Intent broadcast, you must first add the RECEIVE_SMS permission. Add a uses-permission to the application manifest, as shown in the following snippet:

 

<Uses-permission android: name = "android. permission. RECEIVE_SMS"/>

 

The SMS broadcast Intent contains details about the new SMS. To extract the SmsMessage object array packaged in the Bundle of the SMS broadcast Intent, use the pdus key to extract the SMS pdus array. Each object represents an SMS message. Convert each pdu byte array into a SmsMessage object and call SmsMessage. createFromPdu to input each byte array, as shown in the following snippet:

 

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]);

}

 

Each SmsMessage object contains the details of the SMS message, including the source address (mobile phone number), time, and message body.

 

The following example demonstrates how a Broadcast Receiver implements the onReceive function to check whether the new SMS starts with the @ echo string. If yes, send the same text to the mobile phone:

 

Public class IncomingSMSReceiver extends BroadcastReceiver

{

Private static final String queryString = "@ echo";

Private static final String SMS_RECEIVED = "android. provider. Telephony. SMS_RECEIVED ";

 

Public void onReceive (Context _ context, Intent _ 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 ();

If (msg. toLowerCase (). startsWith (queryString ))

{

String out = msg. substring (queryString. length ());

Sms. sendTextMessage (to, null, out, null, null );

}

}

}

}

}

}

 

To listen to text messages, use the Intent Filter to register the Broadcast Receiver and enable it to listen to the android. provider. Telephony. SMS_RECEIVED action, as shown in the following snippet:

 

Final String SMS_RECEIVED = "android. provider. Telephony. SMS_RECEIVED ";

IntentFilter filter = new IntentFilter (SMS_RECEIVED );

BroadcastReceiver receiver ER = new IncomingSMSReceiver ();

RegisterReceiver (receiver, filter );
How does android monitor the text message content sent by the program?

First, define two broadcast receivers: BroadcastReceiver
/** Broadcast sent and received **/String SENT_SMS_ACTION = "SENT_SMS_ACTION"; String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION "; // broadcasting private BroadcastReceiver sendMessage = new BroadcastReceiver () {@ Overridepublic void onReceive (Context context, Intent intent) {// determine whether the message is successfully sent. switch (getResultCode () {case Activity. RESULT_ OK: // message sent successfully default: break ;}}; // broadcast private BroadcastReceiver receiver ER = new BroadcastReceiver () {@ Overridepublic void onReceive (Context context, Intent intent) {// indicates that the recipient has successfully received the SMS}; then, do not forget to register the two broadcast receivers defined above in the initialization method. The registration code is: registerReceiver (sendMessage, new IntentFilter (SENT_SMS_ACTION); registerReceiver (receiver er, new IntentFilter (DELIVERED_SMS_ACTION ));
 
In android, how can I listen to text message events? If I press the text message button, other functions defined by myself will be executed.


<Map name = "Map" id = "Map">
<Area shape = "poly" coords = "567,417,562,420,557,420,552,422,552,431,549,437,545,442,546,452,538,460,538,468,543,473,551,483,556,486,563,492,574,477,581,468,588,455,589,441,597,429,594,425,586,423,577,425,574,423,569,415,580,441,580,422" href = "img/onepiece.jpg"/>
<Area shape = "poly" coords = "619,456,617,459,610,471,606,478,604,487,604,497,610,506,615,511,621,496,626,472,626,461,617,479" href = "test3.html"/>

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.