Send SMS using Intent

0
62

Hello, Developers!! This Application will demonstrate how to send SMS using Intent.

Output:

Download Source code: [media-downloader media_id=”1179″ texts=”SMS.ZIP”]

Download APK file: [media-downloader media_id=”1181″ texts=”SMS apk”]

Wants to Learn Advanced Android Application development from scratch- Beyond Basics

 

Creating New Project – SMS

Open your Android Studio & create a new Project, have taken Empty Activity for this project. We kept all the things by default and clicked finish.

Manifest Declarations

To send SMS we need to add following permission in the AndroidManifest.xml

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

Creating Layout

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mytrendin.sms.MainActivity">

    <TextView
        android:text="Enter Phone Number:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/tv_num" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:layout_below="@+id/tv_num"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/et_ph"
        android:hint="Enter Number" />

    <TextView
        android:text="Enter Your Message:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_ph"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="22dp"
        android:id="@+id/tv_msg" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:ems="10"
        android:layout_below="@+id/tv_msg"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="12dp"
        android:id="@+id/et_msg"
        android:hint="Enter Text" />

    <Button
        android:text="SEND"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/btn_send" />
</RelativeLayout>

Creating MainActivity.java

package com.mytrendin.sms;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   Button bt_send;
   EditText phNo,msg;
   Boolean permission = false;
   public static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 99;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_send = (Button)findViewById(R.id.btn_send);
        phNo = (EditText)findViewById(R.id.et_ph);
        msg = (EditText)findViewById(R.id.et_msg);

        bt_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (android.os.Build.VERSION.SDK_INT >= 
                    Build.VERSION_CODES.M) {
                    permission = checkSmsPermission();
                    if(permission)
                        sendSms();
                    else
                        permission =checkSmsPermission();
                }
                else
                    sendSms();

            }
        });
    }

    void sendSms(){
        String phoneNumber = phNo.getText().toString();
        String message = msg.getText().toString();
       Uri uri = Uri.parse("smsto: "+ phoneNumber);
        Intent send_msg = new Intent(Intent.ACTION_SENDTO, uri);
        send_msg.putExtra("sms_body",message);
        startActivity(send_msg);
    }

    public boolean checkSmsPermission() {

        if (ContextCompat.checkSelfPermission(this,
                android.Manifest.permission.SEND_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.
                    shouldShowRequestPermissionRationale(this,
                    android.Manifest.permission.SEND_SMS)) {
                ActivityCompat.requestPermissions(this,
                        new String[]{
                        android.Manifest.permission.SEND_SMS},
                        MY_PERMISSIONS_REQUEST_SEND_SMS);
            } else {
                ActivityCompat.requestPermissions(this,
                        new String[]{
                        android.Manifest.permission.SEND_SMS},
                        MY_PERMISSIONS_REQUEST_SEND_SMS);
            }
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                 String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_SEND_SMS: {
                if (grantResults.length > 0
                        && grantResults[0] == 
                         PackageManager.PERMISSION_GRANTED) {
                    if (ContextCompat.checkSelfPermission(this,
                         android.Manifest.permission.SEND_SMS)
                         == PackageManager.PERMISSION_GRANTED){

                            sendSms();
                        return;
                    }
                } else {
                    Toast.makeText(this, "permission denied",
                            Toast.LENGTH_LONG).show();
                }
                return;
            }
        }
    }
}

When the button to send SMS will be clicked we will apply a Version check before executing our code. If the Android Version is equal or above the Marshmallow version then permission has to be granted first, by the user.

 bt_send.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    permission = checkSmsPermission();
                    if(permission)
                        sendSms();
                    else
                        permission =checkSmsPermission();
                }
                else
                    sendSms();

        }
});

Related:

Send SMS using Intent

How to make a Call using Intent

What is the difference between IntentService and Service?

How to send data from one activity to another using getintent() ?

How to send the data from on activity to another using Bundle() ?

sendSms method:

we will first retrieve the phone number and message entered by the user.Then the phone number will be

void sendSms(){
        String phoneNumber = phNo.getText().toString();
        String message = msg.getText().toString();
       Uri uri = Uri.parse("smsto: "+ phoneNumber);
        Intent send_msg = new Intent(Intent.ACTION_SENDTO, uri);
        send_msg.putExtra("sms_body",message);
        startActivity(send_msg);
}

Uniform Resource Identifier(URI) is a string of characters used to identify a resource.
The method Uri.parse creates a new Uriobject from a properly formatted String which we can use to refer to resources
Then this Uriobject is passed to the Intent along with the action that we want to perform to send SMS which in our case is Intent.ACTION_SENTO
The message to be sent will be added to the intent using

send_msg.putExtra("sms_body",message);

and finally, pass the Intent object to the startActivity method to perform the action or send SMS. If You are Beginner Learn Android application development from the scratch


If you really liked the article, please subscribe to our YouTube Channel for videos related to this article.Please find us on Twitter and Facebook.

LEAVE A REPLY

Please enter your comment!
Please enter your name here