Login     Sign Up
Innocent Cyril (@admin)
3 months ago
30 Views

The Android debugging bridge supports sending SMS messages between multiple emulator instances. To send an SMS from one emulator to another, specify the port number of the target emulator as the “to” address when sending a new message.

Android will automatically route your message to the target emulator instance, where it’ll be handled as a normal SMS.

Conforming to the Maximum SMS Message Size

SMS text messages are normally limited to 160 characters, so longer messages need to be broken into a series of smaller parts. The SMS Manager includes the divideMessage method, which accepts a string as an input and breaks it into an ArrayList of messages wherein each is less than the allowable size. Use sendMultipartTextMessage to transmit the array of messages, as shown in the snippet below:

ArrayList<String> messageArray = smsManager.divideMessage(myMessage); ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); for (int i = 0; i < messageArray.size(); i++)

sentIntents.add(sentPI);

smsManager.sendMultipartTextMessage(sendTo,

null,

messageArray,

sentIntents, null);

The sentIntent and deliveryIntent parameters in the sendMultipartTextMessage method are ArrayLists that can be used to specify different Pending Intents to fire for each message part.

Sending Data Messages

You can send binary data via SMS using the sendDataMessage method on an SMS Manager. The sendDataMessage method works much like sendTextMessage, but includes additional parameters for the destination port and an array of bytes that constitute the data you want to send.

The following skeleton code shows the basic structure of sending a data message:

Intent sentIntent = new Intent(SENT_SMS_ACTION);

PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0,

sentIntent,

0);

short destinationPort = 80;

byte[] data = [ … your data … ];

smsManager.sendDataMessage(sendTo, null, destinationPort, data, sentPI, null);

Listening for SMS Messages

When a new SMS message is received by the device, a new broadcast Intent is fired with the android.provider.Telephony.SMS_RECEIVED action. Note that this is a String literal, SDK 1.0 does not include a reference to this string so you must specify it explicitly when using it in your applications.

For an application to listen for SMS Intent broadcasts, it first needs to be have the RECEIVE_SMS permis-sion granted. Request this permission by adding a uses-permission tag to the application manifest, as shown in the following snippet:

<uses-permission

android:name=”android.permission.RECEIVE_SMS”/>

The SMS broadcast Intent includes the incoming SMS details. To extract the array of SmsMessage objects packaged within the SMS broadcast Intent bundle, use the pdu key to extract an array of SMS pdus, each of which represents an SMS message. To convert each pdu byte array into an SMS Message object, call SmsMessage.createFromPdu, passing in each byte array as shown in the snippet below:

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 SMS message details, including the originating address (phone number), time stamp, and the message body.

The following example shows a Broadcast Receiver implementation whose onReceive handler checks incoming SMS texts that start with the string @echo, and then sends the same text back to the phone that sent it:

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 incoming messages, register the Broadcast Receiver using an Intent Filter that listens for the android.provider.Telephony.SMS_RECEIVED action String, as shown in the code snippet below:

final String SMS_RECEIVED = “android.provider.Telephony.SMS_RECEIVED”;

IntentFilter filter = new IntentFilter(SMS_RECEIVED); BroadcastReceiver receiver = new IncomingSMSReceiver(); registerReceiver(receiver, filter);