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

Mad 2.3

Uploaded by

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

Mad 2.3

Uploaded by

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

Experiment 2.

3
Student Name: Sameer Ahuja UID: 21BCS1791
Branch: BE-CSE Section/Group: CC-602-A
th
Semester: 6 Date of Performance: 29-02-2024
Subject Name: Mobile Application Development
Subject Code: 21CSP-355

1. Aim:
To create and design an Android application Send SMS using Intent using Android Studio.

2. Objective:
The objective of an Android application using various controls such as Android Studio(Latest
Version)This type of application aims to showcase the usage and functionalities of these UI
elements to enhance the user experience.

3. Procedure:
Step 1: Open Android Stdio and then click on File -> New -> New project.
Step 2: Then type the Application name as smsintent and click Next.
Step 3: Then select the Minimum SDK as shown below and click Next.
Step 4: Then select the Empty Activity and click Next. Finally click Finish.
Step 5: Design layout in activity_main.xml.
Step 6: Send SMS and Display details give in MainActivity file. Step
7: Save and run the application.

4. Script :
MainActivity.java:- package
com.example.smsintent; import
android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.Build; import
android.os.Bundle; import
android.telephony.SmsManager;
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;

public class MainActivity extends AppCompatActivity {


private EditText number, message;
private Button send;
@SuppressLint("HissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); number =
findViewById(R.id.number); message =
findViewById(R.id.text); send =
findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.SEND_SMS) ==
PackageManager.PERMISSION_GRANTED) {
sendSMS();
} else {
requestPermissions(new String[]{Manifest.permission.SEND_SMS}, 1);
}
} else {
sendSMS();
}
}
});
}

private void sendSMS() {


String phoneNo = number.getText().toString().trim();
String SMS = message.getText().toString().trim(); try
{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, SMS, null, null);
Toast.makeText(this, "Message is sent", Toast.LENGTH_SHORT).show();
} catch (Exception e) { e.printStackTrace();
Toast.makeText(this, "Failed to send message", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
sendSMS();
} else {
Toast.makeText(this, "Permission denied. Cannot send SMS.",
Toast.LENGTH_SHORT).show();
}
}
}
}

activity_main.xml
<?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" android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Phone Number"/>

<EditText
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/number"
android:layout_marginTop="16dp"
android:hint="Enter Message"/>

<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text"
android:layout_marginTop="16dp"
android:text="Send SMS"/>

</RelativeLayout>

4. Output:

You might also like