You are on page 1of 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


Experiment 2.3
Student Name: Manohar Chaudhary UID: 21BCS4422
Branch: CSE Section/Group: 905-A
Date of Performance: 05/03/2024 Subject Code: 21CSH-355
Subject Name: Mobile Application Development with Lab Semester: 6th

1. Aim: To design an android application Send SMS using Intent.

2. Objective: The objective of an Android-based application that uses Intent to send SMS can be to
create a convenient and user-friendly tool for sending text messages. This type of app aims to
leverage the Android platform's capabilities to provide a seamless and efficient way for users to
compose and send SMS messages.

3. Input/Apparatus Used:

Input:

Computer: Android Studio is compatible with Windows, macOS, and Linux. Ensure that your
computer meets the minimum system requirements for the chosen operating system.

Internet Connection: A reliable internet connection is required to download Android Studio and the
necessary SDK components during the installation process.

Apparatus:

Android Studio Installer: Download the Android Studio installer from the official Android Studio
website (https://developer.android.com/studio). Choose the appropriate version for your
operating system.

Computer Mouse,Keyboard and Monitor/Display: Use a mouse and keyboard to interact with the
installation process, configure settings, and navigate through Android Studio. Android Studio
requires a monitor or display to visualize the installation process and subsequently to develop
and test Android applications.

Storage Space: Ensure sufficient free storage space on your computer to accommodate the Android
Studio installation and any additional SDK components you may download.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Procedure:
Step 1: First create a new Android Application. This will create an XML file “activity_main.xml” and a
Java File “MainActivity.Java”.

Step 2: Open the “activity_main.xml” file and add the following widgets in a Relative Layout:
• A Editext to the message to send.

• A EditText to enter the phone number

• A Submit to send sms to the entered phone number.

Also, Assign the ID to each of the components along with other attributes as shown in the given
image and the code below. The assigned ID on a component helps that component to be easily
found and used in the Java files.

Step 3: Now, after the UI, this step will create the Backend of Application. For this, open the
“MainActivity.java” file and instantiate the components made in the XML file (Edit Text and Submit
Button) using findViewById() method. This method binds the created object to the UI Components
with the help of the assigned ID.

5. Source Code:

The Main Activity File package


com.example.exprimentsix; import
android.Manifest; import
android.content.Intent;
import android.content.pm.PackageManager;
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.annotation.NonNull;
import
androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat; import
androidx.core.content.ContextCompat;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public class MainActivity extends AppCompatActivity {

private static final int PERMISSION_REQUEST_CODE = 1;


private EditText phoneNumberEditText; private EditText
messageEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
phoneNumberEditText = findViewById(R.id.phoneNumberEditText); messageEditText =
findViewById(R.id.messageEditText);

Button sendButton = findViewById(R.id.sendButton);


sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { if
(ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new
String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST_CODE); return;
}
sendSMS();
}
}); }

private void sendSMS() {


String phoneNumber = phoneNumberEditText.getText().toString(); String
message = messageEditText.getText().toString();

Intent smsIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" +


phoneNumber)); smsIntent.putExtra("sms_body", message);
startActivity(smsIntent);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
grantResults) { super.onRequestPermissionsResult(requestCode,
permissions, grantResults); if (requestCode ==
PERMISSION_REQUEST_CODE) { if (grantResults.length > 0 &&
grantResults[0] ==
PackageManager.PERMISSION_GRANTED) { sendSMS();
} else {
// Permission denied
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
}
}
}
The Layout File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">

<EditText android:id="@+id/phoneNumberEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Phone
Number" android:inputType="phone"/>

<EditText android:id="@+id/messageEditText"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_below="@id/phoneNumberEditText
" android:hint="Message"/>

<Button android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/messageEditText"
android:layout_centerHorizontal="true" android:text="Send
SMS"/>

</RelativeLayout>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

6. Output:

( 9 8 7) 6 5 4 - 3 2 1 1

8 8 73 8 8 1 5 3 7

Hii my name is Murli

Hii my name is Murli

7. Observations/Outcomes:

1. Learned how to button in Android studio.


2. Learned how to send sms using intent in android studio
3. Learned about giving SEND-SMS permission
4. Learned how to handle on click event on button.

You might also like