Sending a Text Message Over the Phone Using SmsManager in Android - GeeksforGeeks

23 Dec, 2021
This article is about sending a text SMS over the phone using the SMSManager class in an Android application. For this, a basic knowledge of the fundamentals of android app development, creating a new project,  running an android app,  Views, and handling of click event buttons is required.
SMSManager class manages operations like sending a text message, data message, and multimedia messages (MMS). For sending a text message method sendTextMessage() is used likewise for multimedia message  sendMultimediaMessage() and for data message sendDataMessage() method is used. The details of each function are:
Function
Description
Below is an example of a basic application that sends a text message. 
 
Step 1: Create a new Android Application.
Step 2: Go to AndroidManifest.xml.
app->Manifest->AndroidManifest.xml
 
Step 3: In AndroidManifest.xml add the permission to send SMS. It will permit an android application to send SMS.
<uses-permission android:name=” android.permission.SEND_SMS ” /> 
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:dist="http://schemas.android.com/apk/distribution"    package="com.example.gfg">    <uses-permission android:name="android.permission.SEND_SMS"/>    <dist:module dist:instant="true" />      <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>  </manifest>
 Step 4: Open activity_main.xml from the following address and add the below code. Here, in a Linear Layout, two edittext for taking phone number and a text message and a button for sending the message is added.
app->res->layout->activitymain.xml
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:orientation="vertical"    android:layout_marginTop="140dp"    android:layout_height="match_parent"    tools:context=".MainActivity">        <EditText        android:id="@+id/editText"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10"        android:hint="Enter number"        android:inputType="textPersonName" />      <EditText        android:id="@+id/editText2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10"        android:hint="Enter message"        android:inputType="textPersonName" />      <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:layout_marginLeft="60dp"        android:layout_marginRight="60dp"        android:text="SEND" /></LinearLayout>
 Step 5: Open MainActivity.java
app->java->com.example.gfg->MainActivity
Create objects for views used i.e.,  editTexts and button. In the onCreate method find all the views using the findViewById() method. 
Viewtype object =(ViewType)findViewById(R.id.IdOfView);
Since the Send button is for sending the message so onClickListener is added with the button. Now create two string variables and store the value of editText phone number and message into them using method getText() (before assigning convert them to a string using toString() method). Now in try block create an instance of SMSManager class and get the SMSManager associated with the default subscription id. Now call method sendTextMessage() to send message.
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(number,null,msg,null,null);
Then display the toast message as “Message sent ” and close the try block. At last in catch block display a toast message because the message was not sent if the compiler executes this block of the code. 
MainActivity.java
package com.example.gfg;  import androidx.appcompat.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 {    EditText phonenumber,message;    Button send;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        send=findViewById(R.id.button);        phonenumber=findViewById(R.id.editText);        message=findViewById(R.id.editText2);        send.setOnClickListener(new View.OnClickListener() {              public void onClick(View view) {                String number=phonenumber.getText().toString();                String msg=message.getText().toString();                try {                    SmsManager smsManager=SmsManager.getDefault();                    smsManager.sendTextMessage(number,null,msg,null,null);                    Toast.makeText(getApplicationContext(),"Message Sent",Toast.LENGTH_LONG).show();                }catch (Exception e)                {                    Toast.makeText(getApplicationContext(),"Some fiedls is Empty",Toast.LENGTH_LONG).show();                }            }        });    }}
 
Note: For running application in Android device enable the SMS permission for the app.
Goto permissions->SMS->YourApp and enable permission.
Output:
Sending the message
Sent message in the messaging app
android
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Create and Add Data to SQLite Database in Android?
Resource Raw Folder in Android Studio
Broadcast Receiver in Android With Example
Services in Android with Example
Android RecyclerView in Kotlin
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples