when the device receives a new SMS message, a Android.provider.Telephony.SMS_RECEIVED action is broadcast with a Intent . Note that this action is a string value, andtheSDK 1.0 no longer contains a reference to the string, so in your application you need to explicitly specify it.
for application monitoring SMS Intent broadcast, you first need to add receive_sms permissions. by Adding a uses-permissionto the application manifest, as shown in the following fragment:
<uses-permission android:name="Android.permission.RECEIVE_SMS"/>
SMSBroadcastIntentcontains a newSMSthe details. In order to extract the packaging inSMSBroadcastIntentof theBundlein theSmsmessageobject array, using thePDUs Keyto extractSMS PDUsarray, where each object represents aSMSmessage. Will eachPDUThe byte array is transformed intoSmsmessageobject, callingSMSMESSAGE.CREATEFROMPDU, pass in each byte array, as shown in the following fragment:
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 SMS details of the message, including the source address (phone number), the time and the message body.
The following example shows a Broadcast Receiver realized the OnReceive function to check whether the new text message is @echo string start, if yes, send the same text to the 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 for text messages, use Intent Filter to register Broadcast Receiver , so that it listens for Android.provider.Telephony.SMS_RECEIVED actions, as shown in the following fragment:
final String sms_received = "Android.provider.Telephony.SMS_RECEIVED";
Intentfilter filter = new Intentfilter (sms_received);
Broadcastreceiver receiver = new Incomingsmsreceiver ();
Registerreceiver (receiver, filter);
Android Monitor SMS SMS