From http://www.cnblogs.com/xirihanlin/archive/2009/10/22/1588356.html
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, SDK
1.0 no longer contains references to this string. Therefore, in your application, you need to explicitly specify it.
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 SMS broadcast intent, use 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"); // The PDUS character in this. Get ("PDUS") is a fixed SMS
PDUS array, which cannot be seen in the SDK. You need to view it in the source code.
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 "; // This can not be seen in the SDK, it is necessary to listen to SMS, in the source code to see the android-2.2-froyo/Android/provider/telephony. java
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 for text messages, use intent filter to register Broadcast
Receiver 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 );