Sending a Text Message Over the Phone Using SmsManager in Android
Last Updated :
01 Sep, 2024
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.
What is the use of the SmsManager Class?
SMSManager class manages operations like sending text messages, data messages, and multimedia messages (MMS). For sending a text message method sendTextMessage() is used likewise for multimedia message sendMultimediaMessage() and for data message, the sendDataMessage() method is used.
The details of each function are:
Function | Description |
---|
sendTextMessage() | sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent, long messageId) |
sendDataMessage() | sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent) |
sendMultimediaMessage() | sendMultimediaMessage(Context context, Uri contentUri, String locationUrl, Bundle configOverrides, PendingIntent sentIntent |
Below is an example of a basic application that sends a text message.
Step-by-Step Implementation of Text Message over the Phone
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 " />
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
Below is the code implementation of the activity_main.xml file:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginTop="60dp"
android:src="@drawable/gfg_icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number:"
android:width="165sp"
android:textSize="18sp" />
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Enter number"
android:inputType="phone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message to be Sent:"
android:width="165sp"
android:textSize="18sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:gravity="top|start"
android:hint="Enter message"
android:inputType="textMultiLine" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="32dp"
android:text="SEND" />
</LinearLayout>
Step 5: Working on 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 fields 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
Similar Reads
How to change the Text Color of a Substring in android using SpannableString class?
In this article, we will learn about how to change the text color of a substring of a string. It is easy to change the color of the whole string but to change the color of a substring we have to use a special class SpannableString. But SpannableString class is not really helpful when it comes to cha
2 min read
SmsManager in Android using Jetpack Compose
Many times while building an android application we have to add a feature through which users will be able to directly send SMS from our android application. So in this article, we will take a look at How to send a text message over a phone using SMS Manager in Android using Jetpack Compose. A sampl
6 min read
How to Convert Text to Speech in Android using Kotlin?
Text to Speech App converts the text written on the screen to speech like you have written âHello Worldâ on the screen and when you press the button it will speak âHello Worldâ. Text-to-speech is commonly used as an accessibility feature to help people who have trouble reading on-screen text, but it
3 min read
How to Send SMS in Android using Kotlin?
SMS Manager is a class in Android which is used to send the SMS to a specific contact from the android application. We can send text messages, data messages, and multimedia messages using this class. There are different methods that are provided to send different types of messages. In this article,
4 min read
How to send message on WhatsApp in Android
Whatsapp is the one of most popular messaging App. Many android applications need the functionality to share some messages directly from their app to WhatsApp. For example, if a user wants to share the app or share a message from the app then this functionality comes in use. Either user can send a t
3 min read
How to Add Memes Using API Call in Android?
Application Programming Interface calling is the process of making requests to external web-based services to retrieve or manipulate data. APIs provide a standardized way for different software applications to communicate with each other. It involves sending a request from one application to another
4 min read
How to send message on WhatsApp in Android using Kotlin
Whatsapp is the one of most popular messaging App. Many android applications need the functionality to share some messages directly from their app to WhatsApp. For example, if a user wants to share the app or share a message from the app then this functionality comes in use. Either user can send a t
3 min read
How to Make an Motivational Quotes App in Android using API Call?
In this article, we will see the process of building a Motivational Quotes App for Android that fetches random quotes from an API and displays them to the user. By the end of this article, we will have a Complete Android application that connects to an API, retrieves motivational quotes and author n
5 min read
Best Practices for Using Text in Android
Text is the most widely used element in Android apps. Text is used in the form of a TextView or an EditText. As a result, in order to improve our performance, we must make the greatest use of text. We'll explore some of the best practices for using text on Android in this article. Text DesignText Pe
6 min read
How To Change The Ringtone On Your Android Phone?
The default ringtone on an Android device may not be suitable for you, as it may be too soft, loud, or annoying. Android offers customization features such as changing the ringtone, adding custom ringtones, and setting unique ringtones for specific contacts. In this article, We'll show how to change
4 min read