0% found this document useful (0 votes)
6 views5 pages

Ex 6

This document outlines the steps to create an Android application that sends SMS using Intent. It includes instructions for setting up a new project, designing the layout with EditText fields for phone number and message, adding necessary permissions, and implementing the SMS sending logic in MainActivity.java. The app allows users to enter a phone number and message, and upon clicking the 'Send SMS' button, the default SMS app opens with the pre-filled information.

Uploaded by

tmsubhashvaleti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Ex 6

This document outlines the steps to create an Android application that sends SMS using Intent. It includes instructions for setting up a new project, designing the layout with EditText fields for phone number and message, adding necessary permissions, and implementing the SMS sending logic in MainActivity.java. The app allows users to enter a phone number and message, and upon clicking the 'Send SMS' button, the default SMS app opens with the pre-filled information.

Uploaded by

tmsubhashvaleti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT NO-6

AIM: Design an android application Send SMS using Intent.


Step 1: Create a New Android Project
1. Open Android Studio and create a new project with an Empty Activity template.
2. Set the name of the project to something like SendSms.
3. Choose Java as the programming language and API 21 (Lollipop) or higher as the minimum SDK.

Step 2: Design the Layout


In the layout, we will have:
 An EditText for the phone number.
 An EditText for the message.
 A Button to send the SMS.
res/layout/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:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/phoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter phone number"
android:inputType="phone" />

<EditText
android:id="@+id/messageText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your message"
android:layout_marginTop="16dp" />

<Button
android:id="@+id/sendSmsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"
android:layout_marginTop="24dp" />
</LinearLayout>
Step 3: Add Permissions to Send SMS
In order to send an SMS, we need to request the necessary permissions. Open the AndroidManifest.xml file
and add the following permission:

AndroidManifest.xml: This grants the app permission to send SMS messages.


<uses-permission android:name="android.permission.SEND_SMS" />
Step 4: Implement the Send SMS Logic in MainActivity.java
package com.example.sendsms;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

EditText phoneNumber = findViewById(R.id.phoneNumber);


EditText messageText = findViewById(R.id.messageText);
Button sendSmsButton = findViewById(R.id.sendSmsButton);

sendSmsButton.setOnClickListener(v -> {
String phone = phoneNumber.getText().toString();
String message = messageText.getText().toString();

if (phone.isEmpty() || message.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter all fields", Toast.LENGTH_SHORT).show();
return;
}
// Create an Intent to send SMS
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("sms:" + phone));
intent.putExtra("sms_body", message);
try {
startActivity(intent);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "SMS sending failed", Toast.LENGTH_SHORT).show();
}
});
}
}

Step 5: Running the Application


1. Run the app on your device or emulator.
2. Enter a phone number and a message.
3. Click the "Send SMS" button.
4. The default SMS app will open with the phone number and message pre-filled,
allowing the user to send the SMS.
OUTPUT:

If the user doesn't enter a phone number or a message and clicks the "Send SMS" button,
The app will display
Please enter all fields

You might also like