android-logo

Sending SMS in Android


The default Email app from Android can be used as an email client, so you dont need to develop one from the start. To accomplish this, we need to create an Activity that launches an email client with an implicit Intent and the right action. There are two ways to send p messages using SMSManager API and Intents.


Java syntax for SMSManager API


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


Java syntax for 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"); 


Sending SMS in Android

Below is a sample Java code in Activity file for sending sms using SMSManager.

MainActivity.java


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();
                }
            }
        });
       }
} 

Below is a sample Java code in Activity file for sending sms using Intent.

MainActivity.java


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 Intent
        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:p="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:p="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:p="Send Message" />
</LinearLayout> 

Add permission in AndroidManifest.xml file.

AndroidManifest.xml


<uses-permission android:name="android.permission.SEND_SMS"/>
Output
sending-sms-1

previous-button
next-button

facebook-logo
twitter-logo
instagram-logo

2020 © All rights reserved.