Sending SMS using Android

March 08, 2013

SMS – Short Message Service is very effective and quick way of communication. Your app may have a use case wherein it needs to send SMS to someone. So lets see how it can done, it is relative simple as you would see below:

Step 1: Creating the project
This is the simplest step of them all. Just go to your project wizard and create a new project with the following details:

Project name: SendSms2.3
Application name: SendSms2.3
Package name: send.sms
Minimum Required SDK: API 8
Target SDK: API 10
Main Activity: SendSmsActivity
Layout name: main
Step 2: Editing the Manifest
In order  to allow any app to send or receive SMS you need to allow the app some special permissions. They are:

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

So I added then to the Manifest file and it looked like this:

    <?xml version=”1.0″ encoding=”utf-8″ ?>
<manifest xmlns:android=  package=”send.sms” android:versionCode=”1″ android:versionName=”1.0″>
       <uses-sdk android:minSdkVersion=”10″ />
       <uses-permission android:name=”android.permission.SEND_SMS” />
       <uses-permission android:name=”android.permission.RECEIVE_SMS” />
     <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name”>
<activity android:name=”.SendsmsActivity” android:label= “@string/app_name”>
                   <action android:name=”android.intent.action.MAIN” />
                   <category android:name=”android.intent.category.LAUNCHER” />

Step 3: Creating a layout
UI is most important but I created a very simple user interface to test everything out
SMS Android App Screenshot
It just has 2 text boxes and a button:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<LinearLayout xmlns:android=  android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical”>
<LinearLayout android:layout_width=”fill_parent” android:id=”@+id/linearLayout1″ android:layout_height=”wrap_content” android:layout_gravity=”center_vertical|center_horizontal” android:layout_marginTop=”50px”>
<TextView android:text=”Number :” android:id=”@+id/textView1″ android:layout_width=”100px” android:layout_height=”30px” android:gravity=”center” android:layout_gravity=”center_vertical” android:textSize=”25px” />
<EditText android:layout_marginRight=”0px” android:background=”#FFFFFF” android:textColor=”#000000″ android:layout_marginLeft=”30px” android:id=”@+id/edt_number” android:layout_width=”165px” android:layout_height=”40px” android:layout_gravity=”center_vertical” />
<LinearLayout android:layout_gravity=”top” android:id=”@+id/LinearLayout2″ android:layout_height=”wrap_content” android:layout_width=”fill_parent” android:layout_marginTop=”20dp”>
       <TextView android:id=”@+id/TextView1″ android:layout_width=”100px” android:layout_height=”30px” android:layout_gravity=”fill_vertical” android:gravity=”center” android:text=”Message :” android:textSize=”25px” />
       <EditText android:layout_marginRight=”0px” android:background=”#FFFFFF” android:textColor=”#000000″ android:layout_gravity=”top” android:layout_marginLeft=”30px” android:layout_width=”165px” android:layout_height=”165px” android:id=”@+id/edt_msgtext” />
<LinearLayout android:id=”@+id/LinearLayout3″ android:layout_width=”300px” android:layout_height=”50px” android:layout_gravity=”center” android:layout_marginTop=”20dp”>
       <Button android:layout_height=”50px” android:layout_width=”fill_parent” android:layout_gravity=”top” android:textSize=”25px” android:text=”Send” android:id=”@+id/btn_send” />

Now that it was done I proceeded to more important thing.

Step 4: Writing the activity
I created the activity like this:

package send.sms;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SendsmsActivity extends Activity {
    /** Called when the activity is first created. */
    Button buttonSend;
        EditText editNumber,editMessageText;
    public void onCreate(Bundle savedInstanceState) {
        buttonSend = (Button)findViewById(;
        editNumber = (EditText)findViewById(;
        editMessageText = (EditText)findViewById(;
        buttonSend.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            String phoneno = editNumber.getText().toString();
String messagetxt = editMessageText.getText().toString();       
            if (phoneno.length()>0 && messagetxt.length()>0)               
                                sendSMS(phoneno, messagetxt);
                                    “Please enter both phone number and message.”,                                               Toast.LENGTH_SHORT).show();

We should use pending intent to broadcast the message, so I used that and also displayed a simple Toast if no message or phone number is indicated.
And then to send message we use SmsManager in a user defined function sendSms as follows:

private void sendSMS(String phoneNumber, String message)
        String SENT = “SMS_SENT”;
         PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
        SmsManager sms = SmsManager.getDefault(); 
        sms.sendTextMessage(phoneNumber, “kalpen”, message, sentPI,null);

And that is all you need to do to send an SMS using Android API. You can use this to create all sorts of applications For example, you can create an application for assigning Tasks to your employees with a feature to send a text message to them for reminder or alerts.

The complete project is attached to post.

Attachment Size 152.85 KB
Using Location Services in AndroidGame development in Python
Like us on Facebook