Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

DEPARTMENT OF

COMPUTERSCIENCE & ENGINEERING

Experiment - 2.3
Student Name: Priyanshu Ghosh UID: 21BCS8733
Branch: BE-CSE Section/Group: 648 B
Semester: 6 Date of Performance: 1-3-24
Subject Name: MAD Subject Code: 21CSP-355

1. Aim:
Create an Android-based application and use intent to send SMS.

2. Objective:
The objective is to develop an Android application featuring intent that can be used to send SMS.

3. System Requirements:
Microsoft Windows 7/8/10(32 Bit or 64bit)
4GB RAM minimum, 8GB RAM recommended( plus 1GB for the Android Emulator)
2GB of available disk space minimum, 4GB recommended.
(500MB for IDE plus 1.5GB for Android SDK and emulator system image) 1280 x 800
minimum screen resolution

4. Steps:
Step 1: The first step is to use Android Studio to construct a straightforward Android application.

The screen that appears when you click the Android Studio icon is seen below.
DEPARTMENT OF
COMPUTERSCIENCE & ENGINEERING

Step 2: The first step is to use Android Studio to construct a straightforward Android
application. Start a new Android Studio project to begin developing your
application. The name of the application, package details, and project location
should be requested in a new installationframe:

Step 3: Open activity_main.xml and make interface to input mobile number and enter message or text.
DEPARTMENT OF
COMPUTERSCIENCE & ENGINEERING

Step 4: The Triggering of toast is done via MainActivity.java on clicking submit button.

Step 5: Add necessary permissions to be used in the Manifest file. Specifically, SEND_SMS
of telephony
DEPARTMENT OF
COMPUTERSCIENCE & ENGINEERING

5. Script and Output


❖ Script of Main Activity

package com.example.exp6_sms;

import android.Manifest;
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 static final int PERMISSION_REQUEST_CODE = 100;
EditText phoneNumberEditText, messageEditText;
Button sendSMSButton;

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

phoneNumberEditText = findViewById(R.id.editText);
messageEditText = findViewById(R.id.editText2);
sendSMSButton = findViewById(R.id.button);

sendSMSButton.setOnClickListener(new View.OnClickListener() {
@Override
DEPARTMENT OF
COMPUTERSCIENCE & ENGINEERING

public void onClick(View v) {


if (checkPermission()) {
sendSMS();
} else {
requestPermission();
}
}
});
}
private boolean checkPermission() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_GRANTED;
}

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) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
sendSMS();
} else {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}
}
private void sendSMS() {
String phoneNumber = phoneNumberEditText.getText().toString();
String message = messageEditText.getText().toString();

try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Failed to send message",
Toast.LENGTH_LONG).show();
}
}
}
DEPARTMENT OF
COMPUTERSCIENCE & ENGINEERING

❖ Manifest file
<uses-feature
android:name="android.hardware.telephony"
android:required="false" />
<uses-permission android:name="android.permission.SEND_SMS" />

Output:

1234567890

6. Learning Outcomes:
1. Learned how to use Intent in my application.
2. Learned how to incorporate and utilize drawable resources like images within the app.
3. Wrote code of 5 Example to using checkbox and radio button.
4. Learned how to add image to our android studios.
5. Learned to manage button clicks using the onClick attribute in XML layout files.

You might also like