How to send SMS message in Android

In Android, you can use SmsManager API or device’s Built-in SMS application to send a SMS message. In this tutorial, we show you two basic examples to send SMS message :

  1. SmsManager API
    
    	SmsManager smsManager = SmsManager.getDefault();
    	smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);
    
  2. Built-in SMS application
    
    	Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    	sendIntent.putExtra("sms_body", "default content"); 
    	sendIntent.setType("vnd.android-dir/mms-sms");
    	startActivity(sendIntent);
            

Of course, both need SEND_SMS permission.


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

P.S This project is developed in Eclipse 3.7, and tested with Samsung Galaxy S2 (Android 2.3.3).

Note
The Built-in SMS application solution is the easiest way, because you let device handle everything for you.

1. SmsManager Example

Android layout file to textboxes (phone no, sms message) and button to send the SMS message.

File : res/layout/main.xml


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

    <TextView
        android:id="@+id/textViewPhoneNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Phone Number : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextPhoneNo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:phoneNumber="true" >
    </EditText>

    <TextView
        android:id="@+id/textViewSMS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter SMS Message : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextSMS"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:lines="5"
        android:gravity="top" />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send" />

</LinearLayout>

File : SendSMSActivity.java – Activity to send SMS via SmsManager.


package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SendSMSActivity extends Activity {

	Button buttonSend;
	EditText textPhoneNo;
	EditText textSMS;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		buttonSend = (Button) findViewById(R.id.buttonSend);
		textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
		textSMS = (EditText) findViewById(R.id.editTextSMS);

		buttonSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			  String phoneNo = textPhoneNo.getText().toString();
			  String sms = textSMS.getText().toString();

			  try {
				SmsManager smsManager = SmsManager.getDefault();
				smsManager.sendTextMessage(phoneNo, null, sms, null, null);
				Toast.makeText(getApplicationContext(), "SMS Sent!",
							Toast.LENGTH_LONG).show();
			  } catch (Exception e) {
				Toast.makeText(getApplicationContext(),
					"SMS faild, please try again later!",
					Toast.LENGTH_LONG).show();
				e.printStackTrace();
			  }

			}
		});
	}
}

File : AndroidManifest.xml , need SEND_SMS permission.


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

    <uses-sdk android:minSdkVersion="10" />

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

    <application
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SendSMSActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

See demo :

send sms message via smsmanager

2. Built-in SMS application Example

This example is using the device’s build-in SMS application to send out the SMS message.

File : res/layout/main.xml – A button only.


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

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send" />

</LinearLayout>

File : SendSMSActivity.java – Activity class to use build-in SMS intent to send out the SMS message.


package com.mkyong.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SendSMSActivity extends Activity {

	Button buttonSend;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		buttonSend = (Button) findViewById(R.id.buttonSend);

		buttonSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					
				     Intent sendIntent = new Intent(Intent.ACTION_VIEW);
				     sendIntent.putExtra("sms_body", "default content"); 
				     sendIntent.setType("vnd.android-dir/mms-sms");
				     startActivity(sendIntent);
				        
				} catch (Exception e) {
					Toast.makeText(getApplicationContext(),
						"SMS faild, please try again later!",
						Toast.LENGTH_LONG).show();
					e.printStackTrace();
				}
			}
		});
	}
}

See demo :

send sms via build-in sms application
send sms via build-in sms application

Download Source Code

Download it – 1. Android-Send-SMS-Example.zip (16 KB)

References

  1. Android SmsManager Javadoc
  2. SMS messaging in Android

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
MB InfoSystem
Guest
MB InfoSystem

Hi!
I’m trying to run the above written program almost the same way you’ve written here but the SMS is not being sent, but I’m getting the (Toast) message that “SMS Sent!”
I’m using android:versionCode=”1″ and android:versionName=”1.0″,
Please help.

Thanks in Advance.
regards,

Mahmoud MISBAH EL-IDRISSI
Guest
Mahmoud MISBAH EL-IDRISSI

how can i receive acknowledgment when i send SMS with eclipse ADT

Vikc
Guest
Vikc

How to handle dual sim phones?

Shreyas C S
Guest
Shreyas C S

If I am implementing either of the above features as one of the fragments, what changes I am supposed to do?

mahdi khatami
Guest
mahdi khatami

hello
i am writing sample program in Android Studio for Sending SMS.
that is good for android version 4.5 and lower than.
and not working in android 5 and upper and fail program.
please help me.

Jay Thummar
Guest
Jay Thummar

whta to do if i have a link of sms service provider for sending bulk message and if want to integrate this with my app to sen d same message to multipale users

Divya
Guest
Divya

hi,
i tried your code but am getting “SMS faild,please try again” i.e the catch part
So what is the solution.Help me out

1111161171159459134
Guest
1111161171159459134

As you may have read, his project has been “tested with Samsung Galaxy S2 (Android 2.3.3).”
Due to change in Google policies, this code should only work on older Android versions.
You shouldn’t expect this code to work on newer Android (i.e. KitKat, Lollipop and Marshmallow).

sri bharath
Guest
sri bharath

SMS can not be sent by Emulator. We should use real android device for sending sms

Vigneswari Annadurai
Guest
Vigneswari Annadurai

hello all,
i am developing a new application. it needs mobile number verification. is anyone know how to send message using SMSGateway(API). Plz help me

Alireza
Guest
Alireza

Hi i am alireza.i can write code to send smsmessage via java but i want to send smsmessage via java code without saving or show in device inbox can you help me??
My email: alireza525354@yahoo.com

ravindra
Guest
ravindra

try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), “SMS Sent!”,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
“SMS faild, please try again later!”,
Toast.LENGTH_LONG).show();
e.printStackTrace();
}

Enamul Haque
Guest
Enamul Haque

Your are nice. when i feel any problem then i look over you site. Sms send is one of the best.
My question is that I have made this. But problem is that when i send sms to any mobile it cut off some money from my balance.
Isn’t sms send is free in android?

Moharram Ali
Guest
Moharram Ali

My emulator running properly by this code, but the number is not finding any SMS
. Does this work only on Android phone

Sharok Dastani
Guest
Sharok Dastani

How can I prevent the SmsManager from adding it to the device SMS history? I am trying to make an app that send SMS in the background. But I don’t want the sent SMS to be added to the SMS history. I am using the SmsManager (using the following code) but the sent message gets added to the history.

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(“phoneNo”, null, “sms message”, null, null);

How can I prevent it from adding the sent SMS to the device SMS history?

sneha
Guest
sneha

hello, iam trying to build an app by which we can send a notification free of cost. please help me

CrazyDroid
Guest
CrazyDroid

thank you! works like a charm!

Dalton Spiderr
Guest
Dalton Spiderr

Very good man!!

when the button is pressed, how to make send a fixed message to a fixed number? nothing editable! just a single button?

Thank you!

Gowthami
Guest
Gowthami

Hello.. I use to try this code.. but the number which i mentioned is not receving SMS .. code is working fine and Toast’s the message which i declared.. is there any format to giv e the number whiel entering.. please help me out.. please.. :-(

Arslan Ali Awan
Guest
Arslan Ali Awan

enter the country code too then it will work

vishnu ram
Guest
vishnu ram

Is there any specific format for example 91-989932.. or 9198999 with no special charachters?I am not getting if I write num with no special charachters.Pls help me out

Vino
Guest
Vino

use trim method along with ur string(phoneno) it’ll change 91-943 to 91943.. Hope this help you :)

Mobisheer Pn
Guest
Mobisheer Pn

good tutorial

Guest
Guest
Guest

ya

bidhu
Guest
bidhu

How to send sms by choosing contact from contact list.. plz help me

Arslan Ali Awan
Guest
Arslan Ali Awan
androida
Guest
androida

hello thks a lot for your tutoriel
I’m trying to develope android application and i need to receive the sms in my application not in message application how can i doo that heeelp please

Hasan Rahman Sawan
Guest
Hasan Rahman Sawan

Simple and great! Thanks very much!

gaurav singh
Guest
gaurav singh

it’s working fine for short message (at least 67 character) after that not working even i used multipart function of SmsManager class. pls help…..

Saurabh Singh
Guest
Saurabh Singh

thanks for the tutorial mkyong.

Hossein
Guest
Hossein

Thank you very much,
How we can recive a SMS and use its text and tel number?

navni
Guest
navni

Using sendTextMessage() method we can send message and it works fine.but how cal i sent free sms .I make birthday wish app so message must be sent free .please give me suggest.

thanh
Guest
thanh

hi all!

– On Built-in SMS application Example, I want default sent to the phone number how? in the above example only sms_body default.

plessee help me………….

Boon
Guest
Boon

Add this:
sendIntent.putExtra(“address”, “0123456789”);

kartheek
Guest
kartheek

Intent sendintent=new Intent(android.content.Intent.ACTION_VIEW,Uri.Parse(“smsto:number”));
StartActivity(sendintent);

S!dd D
Guest
S!dd D

good work buddy…. :)

Twinkle
Guest
Twinkle

Thank you very much! Both codes work for me :)

hitesh
Guest
hitesh

Did you recieve the message on other phone ????

Sandeep Chavarkar
Guest
Sandeep Chavarkar

The message is not sent to other number. Only the Toast says ‘SMS Sent’.

trackback
android app is not sending sms automatically on real device | Android Questions

[…] to this tutorial (and Mo Al Sh’s answer) there’s another built in way of sending SMS’es you could […]