Send SMS in Android Example

Send SMS in android, we can send text messages in two ways, using either SMSManager API or Intents.

// Using SMSManager API

SmsManager s = SmsManager.getDefault();
s.sendTextMessage(number,null,Message,null,null); 
// Using Intents

Intent i = new Intent(Intent.ACTION_VIEW);
i.putExtra("address", new String[]{txtMobile.getText().toString()});
i.putExtra("sms_body",txtMessage.getText().toString());
i.setType("vnd.android-dir/mms-sms"); 
// MainActivity.java

// Using sms manager

package com.androidaura.sms;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
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 MainActivity extends AppCompatActivity {

    private EditText mobile;
    private EditText message;
    private Button btnSms;

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

        mobile = (EditText)findViewById(R.id.mobileNo);
        message = (EditText)findViewById(R.id.msgText);
        btnSms = (Button)findViewById(R.id.btnSend);


        // Using sms manager
        btnSms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    SmsManager smgr = SmsManager.getDefault();
                    smgr.sendTextMessage(mobile.getText().toString(),null,message.getText().toString(),null,null);
                    Toast.makeText(MainActivity.this, "SMS Sent", Toast.LENGTH_SHORT).show();
                }
                catch (Exception e){
                    Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
                }
            }
        });
       }
} 

Another Way Using Intent

// Another Way Using Intent

// Using Intent

package com.androidaura.sms;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
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 MainActivity extends AppCompatActivity {

    private EditText mobile;
    private EditText message;
    private Button btnSms;

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

        mobile = (EditText)findViewById(R.id.mobileNo);
        message = (EditText)findViewById(R.id.msgText);
        btnSms = (Button)findViewById(R.id.btnSend);


        btnSms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse("smsto:"));
                    i.setType("vnd.android-dir/mms-sms");
                    i.putExtra("address", new String(mobile.getText().toString()));
                    i.putExtra("sms_body",message.getText().toString());
                    startActivity(Intent.createChooser(i, "Select:"));
                }
                catch(Exception e){
                    Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
} 
// activity_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="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="20sp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Mobile No" />

    <EditText
        android:id="@+id/mobileNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Message" />

    <EditText
        android:id="@+id/msgText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" />
    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Send Message" />
</LinearLayout>

 

Add android.permission.SEND_SMS permission in AndroidManifest.xml file

// AndroidManifest.xml

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

Output