Search
  • Sushree Barsa Pattnayak

SMS Enabling Technique In mobile phones

We all have messaging apps for instant messaging which are providing a platform for enabling conversational chat, chatbots, messaging apps etc. Some popular messaging apps are WhatsApp, Facebook messenger, Snapchat, Telegram. Most of them use Internet connection whereas SMS (Short Message Service) allows mobile phone users to send text messages without Internet connection .SMS is more dominant form of communication. Do you know how this thing can be done in android? So here is a blog which will give you an overall idea how mobile devices are able to exchange short text messages.

Android provides SmsManager API or devices built in SMS applications to send SMS. To use this application in android this API must be added in the manifest file as follows-


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

The xml file i have designed for sending the message to specific user using mobile phone number, more features may be added to the designing also...

The xml coding i have developed as follows-


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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:padding="20dp"

tools:context=".SmsActivity">


<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/txt_title"

android:textColor="@color/colorPrimaryDark"

android:hint="Drop an SMS to whom you want to visit.."

android:textSize="17dp"

android:textStyle="bold"

>


</TextView>

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter Phone Number"

android:textSize="22sp"

android:id="@+id/txt_phone"

android:layout_below="@+id/txt_title"

android:layout_marginTop="15dp"


android:inputType="phone"


>


</EditText>


<EditText

android:layout_width="match_parent"

android:layout_height="300dp"

android:hint="Type message here"

android:textSize="22sp"

android:id="@+id/txt_sms"

android:layout_below="@+id/txt_phone"

android:layout_marginTop="15dp"


android:gravity="top"

android:inputType="textMultiLine"


>


</EditText>

<Button

android:layout_width="match_parent"

android:layout_height="60dp"

android:text="send"

android:id="@+id/send_btn"

android:layout_marginTop="10dp"

android:textSize="25sp"

android:onClick="sms_btn"

android:layout_below="@+id/txt_sms">


</Button>



</RelativeLayout>


The java file will be used to add SmsManagr API in the devices and also to allow user to use this application.The java code is as follows-


package com.example.pass;


import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;

import androidx.core.content.ContextCompat;


import android.Manifest;

import android.app.ProgressDialog;

import android.content.Intent;

import android.content.pm.PackageManager;

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 SmsActivity extends AppCompatActivity {

private EditText txtPhone, txtSms;



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_sms);

txtPhone = findViewById(R.id.txt_phone);

txtSms = findViewById(R.id.txt_sms);


}


public void sms_btn(View view) {

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},0);

}

}


private void MyMessage()

{

String phoneNumber = txtPhone.getText().toString().trim();

String Message = txtSms.getText().toString().trim();

if (!txtPhone.getText().toString().equals("")|| !txtSms.getText().toString().equals(""))

{

SmsManager sms = SmsManager.getDefault();

sms.sendTextMessage(phoneNumber, null, Message, null, null);

Toast.makeText(this, "Message Sent", Toast.LENGTH_SHORT).show();



}

else

{

Toast.makeText(this, "Please Enter Phone number or Message", Toast.LENGTH_SHORT).show();

}






}


@Override

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);


switch (requestCode) {


case 0:

if (grantResults.length >=0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){



MyMessage();

}

else {

Toast.makeText(this, "You don't have required permission", Toast.LENGTH_SHORT).show();

}

break;


}

}

}

The layout of the app will be as per the xml file and we can put conditions in Java file


The app will look as above figure.After entering phone number and message you can click on send

button then the SMS will be sent.


The SMS will appear inside inbox messages as follows-


References-

codingcafe.in

Google API


Written by-

Sushree Barsa Pattnayak

Intern CoE-AI ,CET,BBSR


Brought to you by-

CoE-AI(CET-BBSR)-An initiative by CET-BBSR,Tech Mahindra and BPUT to provide to solutions to real world problems through ML and IoT

www.coeaibbsr.in

https://coeai.19.wix.com/

https://www.facebook.com/CoEAIBBSR/











12 views