Sending and Receiving SMS in Android

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, 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.

12 Comments

Add yours →

  1. 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?

  2. 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

  3. Hi Krishnaraj or Varma ? I don’t know what’s your given name 😉

    First of all… This is a great Blog with good tutorials. I find it by searching something about this topic.

    I have an program which gets an SMS with Datacode.
    These Code stand for information which I give out on my display…

    I searching all the web, maybe you have an idea.

    Is it possible, that my program disable the sms icon in the status bar?

    Maybe you have an idea.

    Great work! –> good stuff for beginners or people with less time 😉

    • Hi Sebastian, disabling the SMS icon in the status bar is not possible in straight forward way. The setResultData(null); and abortBroadcast(); may not display the notification icon, but not guaranteed.

  4. i will try your hole code for Sending and Receiving SMS in Android but only text sms is working but binary sms is not able to receive using this code please send me solution for that i will try for send and receive binary sms on specific port from last 15 days
    so please give me solution

  5. Hi,
    I think that the solution is in using abortBroadcast() and also giving high priority in : intent-filter android:priority=”100″
    Manifest.xml

    public void onReceive(Context context, Intent intent) {

    abortBroadcast();
    // work …
    }

  6. dear sir

    i am a student of ME and i have chosen a project topic in which i have to capture the sms for that i want a source code that will enable mi to capture the sms please help me

    thank you

  7. Hi! Is there a way to send a data message to other non-mobile devices? and since message is converted into bytes. How will I see the message converted into pdu format?

    Thanks!

  8. H i varma,

    I am using sendMultipartTextMessage() , to send my message from my android app.

    At the receiver side native SMS app , i am receiving the text message in parts , which i dont want to because i need to decode the whole message from the SMS i received to my SMS inbox.

    how can i do that ??

Leave a Reply