0% found this document useful (0 votes)
8 views4 pages

2 3madpk

Uploaded by

india Dunia
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)
8 views4 pages

2 3madpk

Uploaded by

india Dunia
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/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


Experiment – 2.3

Student Name: Pranjal Kachhawaha UID: 21BCS3405


Branch: BE-CSE-IT Section/Group: CC-644-
Semester: 6th Date of Performance: 28/02/2024
Subject Name: MAD Lab Subject Code: 21CSH-355

Aim of the practical: To design an android application Send SMS using Intent..

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.

JAVA CODE
public class MainActivity extends AppCompatActivity {

private static final int PERMISSION_REQUEST_CODE = 1;


private EditText editTextMessage;

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

editTextMessage = findViewById(R.id.editTextMessage);
Button btnSendSMS = findViewById(R.id.btnSendSMS);

btnSendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermission()) {
// Permission already granted, proceed with sending SMS
sendSMS();
} else {
// Request permission
requestPermission();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
}
});
}

private void sendSMS() {


String phoneNumber = "123456789"; // Replace with the recipient's phone number
String message = editTextMessage.getText().toString();

Intent smsIntent = new Intent(Intent.ACTION_SENDTO);


smsIntent.setData(android.net.Uri.parse("smsto:" + phoneNumber));
smsIntent.putExtra("sms_body", message);

if (smsIntent.resolveActivity(getPackageManager()) != null) {
startActivity(smsIntent);
} else {
Toast.makeText(this, "SMS app not found", Toast.LENGTH_SHORT).show();
}
}

private boolean checkPermission() {


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_GRANTED;
}
return true;
}

private void requestPermission() {


ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS},
PERMISSION_REQUEST_CODE);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
// Permission granted, proceed with sending SMS
sendSMS();
} else {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}
}
}

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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your message"/>

<Button
android:id="@+id/btnSendSMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextMessage"
android:layout_marginTop="16dp"
android:text="Send SMS" />
</RelativeLayout>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

2. OUTPUT:

3. Learning Outcomes:
• Successful USE of your Android development environment.
• Project Workspace.
• Configuration button Completion.
• How to add new Intent.

You might also like