Android studio SMS sending the application

Hello guys, welcome back to another blog post of Android studio tutorial.

Today we are making an awesome application in android studio which allows you to send SMS through the app like Whatsapp, Telegram, Messanger, etc. I also attached codes related to this application.

Read More –> application using Google speech API

Steps Description
1 create a project in android studio with any name.
2 Modify src/MainActivity.java and add the code
3 Modify layout XML file res/layout/activity_main.xml
4 change the permission in manifests file

Content of src/MainActivity.java

package com.example.module32;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button btns;
    EditText msg,phn;
    private static final int MY_PERMISSION_REQUEST_SEND_SMS = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btns = (Button) findViewById(R.id.btnstart);
        msg = (EditText) findViewById(R.id.txtmsg);
        phn = (EditText) findViewById(R.id.phnumber);
    }
    public void sendsms(View v)
    {
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS);

        if (permissionCheck == PackageManager.PERMISSION_GRANTED)
        {
            MyMessage();
        }
        else
        {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSION_REQUEST_SEND_SMS);
        }
    }

    public void MyMessage()
    {
        String myNumber = phn.getText().toString().trim();
        String myMsg = msg.getText().toString().trim();

        if (myNumber == null || myNumber.equals("") || myMsg == null || myMsg.equals(""))
        {
            Toast.makeText(this,"Please enter some value",Toast.LENGTH_SHORT).show();
        }
        else
        {
            if (TextUtils.isDigitsOnly(myNumber))
            {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(myNumber,null,myMsg,null,null);
                Toast.makeText(this,"Message is sent to the Number",Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(this,"Please Enter Number Only",Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode)
        {
            case MY_PERMISSION_REQUEST_SEND_SMS:
            {
                if (grantResults.length >= 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                    MyMessage();
                }
                else
                {
                    Toast.makeText(this,"You Don't have required to make the Action",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

Content of res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/phnumber"
        android:gravity="start"
        android:textSize="20sp"
        android:hint="Phone Number"
        android:inputType="phone"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtmsg"
        android:gravity="start"
        android:textSize="20sp"
        android:hint="Enter Your Message"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send SMS"
        android:id="@+id/btnstart"
        android:onClick="sendsms"
        android:gravity="center"/>
</LinearLayout>

Edit your manifest file for permission to send SMS

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

Add this line before the start of <application>

Stay Connect with our app: –¬†

https://play.google.com/store/apps/details?id=com.edu.easyengineer

For Assignment Questions and Explanation of a theory topic visit:

http://mycandal.com/mobile-application-devlopmentseit2021/

About easyengineering 38 Articles
Easyengineering.in provides you to all subject and exam related materials online like GPSC, UPSC, IES, GATE, etc. As well as we provide daily job notification, some life-related books and other online courses which are useful too in your study.

Be the first to comment

Leave a Reply

Your email address will not be published.


*