Android SDK Tutorial: How To Send Text Messages

We are going to make a text messaging app: two text fields, one for the phone number and message and of course a send button. Pictures of the interface below.

this is the interface of our sms app

a simple interface for sending text messages

and of course there will also be an error message if the user decides to try to be smart.

An android app showing an error message. One button and two text fields.

The error message users will get if they don't comply.

The JAVA file: SendSMSActivity.java

As always I think my comments in the code are pretty self explanatory, so I’ll just give you the code

package com.example.sendsms;

// import everything you need
import android.app.Activity;
import android.app.PendingIntent;
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 {
	
    Button sendButton;
    EditText phoneTextField;
    EditText msgTextField;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        // load the layout
        setContentView(R.layout.main);        
 
        // make message text field object
        msgTextField = (EditText) findViewById(R.id.msgTextField);
        // make send button object
        sendButton = (Button) findViewById(R.id.sendButton);
        // make phone number field object
        phoneTextField = (EditText) findViewById(R.id.phoneTextField);
        
    }
    
    // this is the function that gets called when you click the button
    public void send(View v)
    {        
    	// get the phone number from the phone number text field
        String phoneNumber = phoneTextField.getText().toString();
        // get the message from the message text box
        String msg = msgTextField.getText().toString();  
        
        // make sure the fields are not empty
        if (phoneNumber.length()>0 && msg.length()>0)                
        {
        	// call the sms manager
            PendingIntent pi = PendingIntent.getActivity(this, 0,
                    new Intent(this, SendSMSActivity.class), 0);                
                SmsManager sms = SmsManager.getDefault();
                // this is the function that does all the magic
                sms.sendTextMessage(phoneNumber, null, msg, pi, null);  
        }
        else
        {
        	// display message if text fields are empty
            Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
        }

        
    }  
}

The code can be easily modified to send automatic text messages.

The Layout File: main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView  
        android:text="To Phone:"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        />     
    <EditText 
        android:id="@+id/phoneTextField"  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"        
        />
    <TextView  
        android:text="Message"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"         
        /> 
            
    <EditText 
        android:id="@+id/msgTextField"  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"       
        />          
    <Button 
        android:text="Send"
        android:id="@+id/sendButton"  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:onClick="send"
        /> 
        
</LinearLayout>

The Manifest File: AndroidManifest.xml

Notice the permission used to send sms, other than that it’s just the default manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.sendsms"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

  	<uses-permission android:name="android.permission.SEND_SMS" />
  
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SendSMSActivity"
                  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>