Using SMS in your Application

Android offers full access to SMS functionality from within your applications with the SMSManager. Using the SMS Manager, you can replace the native SMS application or create new applications that send text messages, react to incoming texts, or use SMS as a data transport layer. 

Sending SMS Messages:

SMS messaging in Android is handled by the SmsManager. You can get a reference to the SMS Manager using the static method SmsManger.getDefault, as shown in the snippet below.

SmsManager smsManager = SmsManager.getDefault(); 


To send SMS messages, your applications require the SEND_SMS permission. To request this permission, add it to the manifest using a uses-permission tag, as shown below: 

<uses-permission android:name=”android.permission.SEND_SMS”/>

Sending Text Messages:

To send a text message, use sendTextMessage from the SMS Manager, passing in the address (phone number) of your recipient and the text message you want to send, as shown in the snippet below: 

String sendTo = “5551234”;
String myMessage = “Sample SMS Text!”; 
smsManager.sendTextMessage(sendTo, null, myMessage, null, null); 

The second parameter can be used to specify the SMS service center to use; entering null as shown in the previous snippet uses the default service center for your carrier. 

The final two parameters let you specify Intents to track the transmission and successful delivery of your messages. 


To react to these Intents, create and register Broadcast Receivers. 

Tracking and Confirming SMS Message Delivery:
To track the transmission and delivery success of your outgoing SMS messages, implement and register Broadcast Receivers that listen for the actions you specify when creating the Pending Intents you pass in to the sendTextMessage method. 

The first Pending Intent parameter, sentIntent, is fired when the message is either successfully sent or fails to send. The result code for the Broadcast Receiver that receives this Intent will be one of: 

❑ Activity.RESULT_OK to indicate a successful transmission. 
❑ SmsManager.RESULT_ERROR_GENERIC_FAILURE To indicate a nonspecific failure. 
❑ SmsManager.RESULT_ERROR_RADIO_OFF When the connection radio is turned off. 
❑ SmsManager.RESULT_ERROR_NULL_PDU To indicate a PDU failure. 


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.
For an application to listen for SMS Intent broadcasts, it first needs to be have the RECEIVE_SMS permission 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. 

To listen for incoming messages, register the Broadcast Receiver using an Intent Filter that listens for the android.provider.Telephony.SMS_RECEIVED

1 comment:

  1. thanks for the information on this blog! I find it very interesting and entertaining! hopefully soon have updates that I love your post! I thank you too!

    Orlando mobile advertising agency

    ReplyDelete