Download source code of the article.
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, ArrayListparts, 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.
Hi varma…
Great tutorial for android begineers…..have a question When i send a binary message using port 8091 as given the message was coming to both SMSDemo as well as Native. I dont understand this behaviour. The Message should reach only to the Specified Port instead it was going to both.So Could you please explain why it is behaving like this OR i have done any mistake while importing?????
Waiting for ur response
Thanks
Chethan
Chethan, normally when you are sending to a particular port it should be visible in Inbox.
Can you please explain the behaviour little bit more? Using the sample application you are trying to send the binary SMS?
In inbox you are seeing it as binary or text?
Hi varma…
Great tutorial for android begineers…..have a question When i send a binary message using port 8091 as given the message was coming to both SMSDemo as well as Native. I dont understand this behaviour. The Message should reach only to the Specified Port instead it was going to both.So Could you please explain why it is behaving like this OR i have done any mistake while importing?????
Waiting for ur response
Thanks
Chethan
Chethan, normally when you are sending to a particular port it should be visible in Inbox.
Can you please explain the behaviour little bit more? Using the sample application you are trying to send the binary SMS?
In inbox you are seeing it as binary or text?
Hi Varma,
Thanks for responding .
Yeah using Sample Application i tried to send a Binary SMS to PORT 8091 . When a message is received at the Destination, Got a Toast message which you have given.
Also the message was there in the Native Inbox(Android Native Messaging App) .The message was in text format.
Normally when we use Specific Port , the Message should reach to that port(Application).
Thanks again
Chethan
Which version of Android you are using?
Hi Varma,
Thanks for responding .
Yeah using Sample Application i tried to send a Binary SMS to PORT 8091 . When a message is received at the Destination, Got a Toast message which you have given.
Also the message was there in the Native Inbox(Android Native Messaging App) .The message was in text format.
Normally when we use Specific Port , the Message should reach to that port(Application).
Thanks again
Chethan
Which version of Android you are using?
Hello KrishnaRaj.
I have been trying to get this to run between two emulators running on 5554 and 5556. But no success. Since I do not have device, any way to confirm this works without testing on the hardware? Please do try and get this to work between two emulators.
Thanks
Hello KrishnaRaj.
I have been trying to get this to run between two emulators running on 5554 and 5556. But no success. Since I do not have device, any way to confirm this works without testing on the hardware? Please do try and get this to work between two emulators.
Thanks
Hello
this is a good tutorial.I am a new beginner in android.This app(downloaded from mobiforge) works fine in between two emulators but how it works on real devices. How they first communicate with other.Can you provide any tutorial for such connections
Thank you.
Hi,
I think that the solution is in using abortBroadcast() and also giving high priority in :
public void onReceive(Context context, Intent intent) {
abortBroadcast();
// work …
}
I tried the binary sms and it works between 2 tmobile phones. But when tried with a HTC droid on verizon, nothing is being recieved or sent.
Also do you consult on developing custom code for android ?
Hi varma…
Great tutorial for android begineers….. I have a question .. I want to get unread messages from inbox. That are work perfectly. But I want to get only 5 messages from inbox then what should I do? At where I should write the query and what is the exact query? Write now I am using this code for getting messges from inbox
Uri uriSMSURI = Uri.parse(“content://sms/inbox”);
Cursor cur = getContentResolver().query(uriSMSURI, null, “read = 0″, null,null);
Please help me I am stuck on this.
hi this is a good helpful for me again thnks for it
hi..Im new to android,im just a leaner,would you please tell me how send sms from desktop to mobiles.
Hi Varma,
Could you make tutorial for multipart messages with method sendMultipartTextMessage().
Im kinda confused how to use this method for sending multipart messages and receiving the following messages.
Thanks
Hi Varma,
Could you make tutorial for multipart messages with method sendMultipartTextMessage().
Im kinda confused how to use this method for sending multipart messages and receiving the following messages..
Thanks