The humble short message service (SMS) celebrates its 20th birthday today. Engineer Neil Papworth sent the first SMS to an Orbitel 901 handset via Vodafone UK’s network in 1992: “Merry Christmas.”
It would be safe to say that nearly every mobile phone sold in the past decade has SMS messaging capabilities. So making use of SMS with your Android app can be a great way to handle information when no Data network (3G or WIFI) is available.
In this article, we will take a look at how you can programmatically send and receive SMS messages in your Android applications.
Sending SMS Data messages
So lets start with the sender AndroidManifest.xml. I recommend you open up the sample code, as I will be highlighting the key parts on this page.
The uses-permission tag will allow the application to send SMS messages. Sending SMS message with the application will potentially incur additional cost on the user’s end.
Indicating the SMS permissions in the AndroidManifest.xml file will let the user decide whether to allow the application to install or not.
Next, we will create an activity called SendActivity that will handle sending SMS messages.
{
PendingIntent sent = this.createPendingResult(SENT, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
String messageText = _message.getText().toString();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendDataMessage(_phoneNumber.getText().toString(), null, SMS_PORT, messageText.getBytes(), sent, null);
}
To send an SMS message, you use the SmsManager class. Unlike other classes, you do not directly instantiate this class; instead you will call the getDefault() static method to obtain an SmsManager object. The sendDataMessage() method sends the SMS message with a PendingIntent. The PendingIntent object is used to identify a target to invoke at a later time. For example, after sending the message, you can use a PendingIntent object to update the current activity, and onActivityResult will be triggered.
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
String toast = null;
switch (requestCode)
{
case SENT :
switch (resultCode)
{
case RESULT_OK :
toast = "OK";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE :
toast = "Generic Failure";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF :
toast = "Radio Off";
break;
case SmsManager.RESULT_ERROR_NULL_PDU :
toast = "Null Pdu";
break;
}
break;
}
if (toast != null)
{
Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
}
}
Receiving SMS Data messages
Besides sending SMS messages, you can also intercept incoming SMS messages using a BroadcastReceiver object.
To see how to receive SMS messages from within your Android application, lets look at the AndroidManifest.xml file within our receiver sample code.
<uses-permission android:name="android.permission.BROADCAST_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
The uses-permission tags will allow the application to receive and read SMS messages. Lets create a class that extends the BroadcastReceiver class and override the onReceive() method:
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
String recMsgString = "";
String fromAddress = "";
SmsMessage recMsg = null;
byte[] data = null;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++)
{
recMsg = SmsMessage.createFromPdu((byte[]) pdus[i]);
try
{
data = recMsg.getUserData();
}
catch (Exception e)
{
}
if (data != null)
{
for (int index = 0; index < data.length; ++index)
{
recMsgString += Character.toString((char) data[index]);
}
}
fromAddress = recMsg.getOriginatingAddress();
}
}
Intent result = new Intent(context, ReceiveActivity.class);
result.putExtra(ReceiveActivity.MESSAGE, "Received SMS from " + fromAddress + ": " + recMsgString);
result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(result);
}
When SMS messages are received, the onReceive() method will be invoked. The SMS message is contained and attached to the Intent object via a Bundle object.
The messages are stored in an Object array in the PDU format. To extract each message, you use the static createFromPdu() method from the SmsMessage class.
The SMS message is then passed to an Activity that shows the message.
That’s it. We’ve managed to send and received a Data SMS within an Android application. Till next time.