Android Messaging Example for Sending SMS Programmatically

Android Messaging Example for Sending SMS Programmatically

Simple way to Send SMS (Text Messages) from an Android Application.

Here we will discuss about how to send SMS from an Android application in a very simple way. Sending SMS from an Android application programmatically is really very easy. Lets have a line by line discussion for how to send massages in Android.

1. Create an Andeoid  project named as : SendSMSExample

2. Add Package Name: com.techblogon.sendsmsexample

3. Select Target SDK version: API 17: Android 4.2 (Jelly Bean)

Note: I am using Eclipse Indigo, all the above data is for the example attached in this post. It might be different options as per your Android setup. But don’t worry it will work for all versions. For more clarity you can download the example for “Android Messaging Example for Sending SMS Programmatically” from buttom of this post.

To make this example very simple, we will take one Android Activity. Then we will take 2 EditText for getting receipent number and composing SMS. Then we will use one more control as a buttom called ‘Send’ to sending the SMS to the destination number. I think this is fair enough to learn how to send the SMS in Android.

1. Add below SMS user permission in your Android Manifest file.

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

Your project’s Manifest file will be looks like below.

AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.techblogon.sendsmsexample”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />
<uses-permission android:name=”android.permission.SEND_SMS” />
<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.techblogon.sendsmsexample.SendSms”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

2. Add below xml file content in your source folder for GUI part. This xml file will hold 2 EditText and One button for sending SMS. In my example the xml file name is activity_send_sms.xml.

Path: \res\layout\

File Name: activity_send_sms.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.SendSms” >

<TextView
android:id=”@+id/textViewTitle”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:text=”Send SMS Programmatically” />

<EditText
android:id=”@+id/editTextEnterReceipents”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”6dp”
android:layout_below=”@+id/textViewTitle”
android:inputType =”number”
android:hint=”Enter Receipent”>
</EditText>

<EditText
android:id=”@+id/editTextCompose”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:layout_below=”@+id/editTextEnterReceipents”
android:layout_above=”@+id/buttonSendNow”
android:layout_marginTop=”6dp”
android:layout_marginBottom=”6dp”
android:gravity=”left|top”
android:hint=”Type your message here” >
</EditText>

<Button
android:id=”@+id/buttonSendNow”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”6dp”
android:onClick=”onClickSend”
android:text=”Send” />

</RelativeLayout>

3. Now coming to java source code for sending SMS from our Android application.

Your project’s Activity.Java file will looks like below. In my example it looks like below code snippet. You can also get the delivery report and error code for the SMS you sent. Please refer to the below code.

Path: \src\com\techblogon\sendsmsexample\

File Name: SendSms.Java

package com.techblogon.sendsmsexample;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class SendSms extends Activity {

public static final String ACTION_SMS_SENT = “com.techblogon.android.apis.os.SMS_SENT_ACTION”;
EditText recipientTextEdit = null;
EditText contentTextEdit =null;
TextView titleTextView =null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_sms);

//Get GUI controls instance from here
recipientTextEdit = (EditText)this
.findViewById(R.id.editTextEnterReceipents);
contentTextEdit = (EditText)this
.findViewById(R.id.editTextCompose);
titleTextView = (TextView)this.findViewById(R.id.textViewTitle);

// Register broadcast receivers for SMS sent and delivered intents
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = null;
boolean error = true;
switch (getResultCode()) {
case Activity.RESULT_OK:
message = “Message sent!”;
error = false;
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
message = “Error.”;
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
message = “Error: No service.”;
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
message = “Error: Null PDU.”;
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
message = “Error: Radio off.”;
break;
}

recipientTextEdit.setText(“”);
contentTextEdit.setText(“”);
titleTextView.setText(message);
titleTextView.setTextColor(error ? Color.RED : Color.GREEN);

}
}, new IntentFilter(ACTION_SMS_SENT));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.send_sms, menu);
return true;
}
public void onClickSend(View v)
{
//Get recipient from user and check for null
if (TextUtils.isEmpty(recipientTextEdit.getText())) {
titleTextView.setText(“Enter Receipent”);
titleTextView.setTextColor(Color.RED);
return;
}

//Get content and check for null
if (TextUtils.isEmpty(contentTextEdit.getText())) {
titleTextView.setText(“Empty Content”);
titleTextView.setTextColor(Color.RED);
return;
}
//sms body coming from user input
String strSMSBody = contentTextEdit.getText().toString();
//sms recipient added by user from the activity screen
String strReceipentsList = recipientTextEdit.getText().toString();
SmsManager sms = SmsManager.getDefault();
List<String> messages = sms.divideMessage(strSMSBody);
for (String message : messages) {
sms.sendTextMessage(strReceipentsList, null, message, PendingIntent.getBroadcast(
this, 0, new Intent(ACTION_SMS_SENT), 0), null);
}
}

}

Note: In the above code snipt, you can also get SMS devilery report and error code if the SMS sending is failed.

4. Now build and run your application on Device and Emulator and enjoy Android coding.

Leave a Reply

Your email address will not be published. Required fields are marked *