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

Practical 29

The document describes a Java program for sending and receiving SMS using a graphical user interface (GUI) in an Android application. It includes the main activity code that handles SMS permissions, user input validation, and SMS sending functionality, along with the corresponding XML layout for the user interface. Additionally, it outlines the necessary permissions in the Android manifest file for sending SMS messages.

Uploaded by

ytube79120
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)
7 views4 pages

Practical 29

The document describes a Java program for sending and receiving SMS using a graphical user interface (GUI) in an Android application. It includes the main activity code that handles SMS permissions, user input validation, and SMS sending functionality, along with the corresponding XML layout for the user interface. Additionally, it outlines the necessary permissions in the Android manifest file for sending SMS messages.

Uploaded by

ytube79120
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/ 4

Practical 29.

1: Write a program to send and receive SMS, make use of following GUI:

Java File:

package com.example.practical29;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


private EditText txtMobile;
private EditText txtMessage;
private Button btnSms;

private static final int REQUEST_SMS_PERMISSION = 1; // Permission request code


private static final String TAG = "SmsDebug"; // Tag for logging

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

// Initialize UI components
txtMobile = findViewById(R.id.mblTxt);
txtMessage = findViewById(R.id.msgtxt);
btnSms = findViewById(R.id.btnSend);

// Check and request SMS permission


requestSmsPermission();

// Set button click listener


btnSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSms();
}
});
}

// Request SMS permission at runtime


private void requestSmsPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, REQUEST_SMS_PERMISSION);
}
}

// Handle runtime permission result


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_SMS_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "SMS Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "SMS Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}

// Send SMS
private void sendSms() {
String mobileNumber = txtMobile.getText().toString().trim();
String message = txtMessage.getText().toString().trim();

// Validate mobile number and message


if (mobileNumber.isEmpty()) {
Toast.makeText(this, "Please enter a mobile number", Toast.LENGTH_SHORT).show();
return;
}

if (!mobileNumber.matches("\\d{10,15}")) { // Regex for valid number


Toast.makeText(this, "Invalid mobile number format", Toast.LENGTH_SHORT).show();
return;
}

if (message.isEmpty()) {
Toast.makeText(this, "Please enter a message", Toast.LENGTH_SHORT).show();
return;
}

try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(mobileNumber, null, message, null, null);
Toast.makeText(this, "SMS Sent Successfully!", Toast.LENGTH_SHORT).show();
Log.d(TAG, "SMS sent to: " + mobileNumber + " | Message: " + message);
} catch (Exception e) {
Toast.makeText(this, "SMS Failed to Send. Please try again.", Toast.LENGTH_SHORT).show();
Log.e(TAG, "SMS sending failed: ", e);
}
}
}

Activity file:

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- Mobile Number Label -->


<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile No"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp" />

<!-- Mobile Number Input -->


<EditText
android:id="@+id/mblTxt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter mobile number"
android:inputType="phone"
app:layout_constraintTop_toBottomOf="@id/fstTxt"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp" />

<!-- Message Label -->


<TextView
android:id="@+id/secTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
app:layout_constraintTop_toBottomOf="@id/mblTxt"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp" />

<!-- Message Input -->


<EditText
android:id="@+id/msgtxt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter your message"
android:inputType="textMultiLine"
android:minLines="4"
android:gravity="top"
app:layout_constraintTop_toBottomOf="@id/secTxt"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp" />

<!-- Send Button -->


<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
app:layout_constraintTop_toBottomOf="@id/msgtxt"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Manifest File:

<?xml version="1.0" encoding="utf-8"?>


<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practical29">

<!-- Required permission for sending SMS -->


<uses-permission android:name="android.permission.SEND_SMS" />

<!-- Add any additional permissions if needed in the future -->


<!-- Example: <uses-permission android:name="android.permission.RECEIVE_SMS" /> -->

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Practical29">

<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

Output:

You might also like