Using SMS


Build your own Android App Dev Empire

Get Instant Access

The Android framework provides full access to SMS functionality using the SmsManager class. Early versions of Android placed SmsManager in the android.telephony.gsm package. Since Android 1.5, where SmsManager supports both GSM and CDMA mobile telephony standards, the SmsManager is now placed in the android.telephony package. Sending an SMS through the SmsManager is fairly straightforward.The steps are

1. Set the permission in the AndroidManifest XML file to send SMS: <uses-permission android:name="android.permission.SEND_SMS">

2. Use the SmsManager.getDefault() static method to get an SMS manager instance: SmsManager mySMS = SmsManager.getDefault();

3. Define the destination phone number for the message and message to send. Use the sendTextMesssage() method to send the SMS to another device:

String destination = "16501234567"; String msg = "Sending my first message";

mySMS.sendTextMessage(destination, null, msg, null, null);

This is sufficient to send an SMS message. However, the three additional parameters in the previous call set to null can be utilized as follows.

■ The second parameter is the specific SMS service center to use. Set to null to use the default service center from the carrier.

■ The fourth parameter is a PendingIntent to track if the SMS message was sent.

■ The fifth parameter is a PendingIntent to track if the SMS message was received.

To use the fourth and fifth parameters, a send message and a delivered message intent need to be declared:


Intent sentIn = new Intent(SENT_SMS_FLAG);

PendingIntent sentPIn = PendingIntent.getBroadcast(this,0,sentIn,0);

Intent deliverIn = new Intent(SENT_SMS_FLAG); PendingIntent deliverPIn

= PendingIntent.getBroadcast(this,0,deliverIn,0);

Then, a BroadcastReceiver needs to be registered for each PendingIntent to receive the result:

BroadcastReceiver sentReceiver = new BroadcastReceiver(){

^Override public void onReceive(Context c, Intent in) { switch(getResultCode()){

case Activity.RESULT_OK:

//sent SMS message successfully; break; default:

//sent SMS message failed break;

BroadcastReceiver deliverReceiver = new BroadcastReceiver(){ ^Override public void onReceive(Context c, Intent in) { //SMS delivered actions

registerReceiver(sentReceiver, new IntentFilter(SENT_SMS_FLAG)); registerReceiver(deliverReceiver, new IntentFilter(DELIVER_SMS_FLAG));

Most SMSes are restricted to 140 characters per text message.To make sure the message is within this limitation, use the divideMessage() method that divides the text into fragments in the maximum SMS message size.Then, the method sendMultipartTextMessage()

should be used instead of the sendTextMessage() method.The only difference is the use of an ArrayList of messages and pending intents:

ArrayList<String> multiSMS = mySMS.divideMessage(msg); ArrayList<PendingIntent> sentlns = new ArrayList<PendingIntent>(); ArrayList<PendingIntent> deliverlns = new ArrayList<PendingIntent>();

for(int i=0; i< multiSMS.size(); i++){ sentlns.add(sentln); deliverlns.add(deliverln);

mySMS.sendMultipartTextMessage(destination, null, multiSMS, sentlns, deliverlns);

Was this article helpful?

0 0
Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment