Sending SMS messages
The class SmsManager is used to send SMS messages in Android. Prior to API Level 4 android.telephony.gsm.SmsManager is used and in API level 4 onwards android.telephony.SmsManager is used. This class can send text, binary and multi-part messages.
Sending text messages
To send a text message, we use the method sendTextMessage. The syntax of the method is:
public void sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
The destinationAddress is the destination phone number, scAddress is the address of the service center (normally we pass null), the sentIntent is a broadcast which will be send when the SMS message is successfully sent from the device. The last parameter deliveryIntent is also a broadcast sent when the SMS message is delivered to the destination address.
Sending multi-part messages
SMS messages can have maximum of 160 characters in length. If you want to send a message longer than 160 characters, you have to split the message and sent one by one. The SmsManager has a convenient method sendMultipartTextMessage which can be used to send messages longer that 160 characters. The syntax is:
public void sendMultipartTextMessage (String destinationAddress, String scAddress, ArrayList parts, ArrayList sentIntents, ArrayList deliveryIntents)
The first and second parameters are same as previous. The third is an ArrayList of strings in the order of original message. When the destination device receives a multi-part message, it combines the message and display to the user. The forth one is an ArrayList of PendingIntent which will broadcast for each string part is send from the device. the fifth one is also an ArrayList of PendingIntent which will broadcast after the string part is delivered to the destination device.
To split the message in to different parts the SmsManager class has a method divideMessage. This function divides the entire message into several parts. Each part contains the maximum SMS message size.
Sending binary SMS message
To send a binary message, we use the method sendDataMessage. The syntax is:
public void sendDataMessage (String destinationAddress, String scAddress, short destinationPort, byte data, PendingIntent sentIntent, PendingIntent deliveryIntent)
The first and second parameters are same as previous methods, the third one is the destination port. The message will be sent to this port instead of the default one. The forth parameter is an array of byte which the message data to send. The fifth and sixth are pending intents to broadcast when the message is send and delivered respectively.
The system will not process the binary message to a specific port. You need an application that will receive the message to the specified port. Please refer to the sample application which receives both binary and text messages.
Please note that there is no method to send multi-part binary messages.
Receiving SMS message
To receive SMS message we create a broadcast receiver and register. The system will broadcast when a message is received. To receive text message we create broadcast receiver with action android.provider.Telephony.SMS_RECEIVED
To receive a binary message we create broadcast receiver with android.intent.action.DATA_SMS_RECEIVED. We also need to specify additional parameters, the receiving data port and the scheme.
The sample application for this article sends both binary and text messages. The application also has two broadcast receivers for both types.