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

29

The document describes code for an Android application that allows sending and receiving SMS messages. It includes XML layout files for the user interface, Java code for the main activity class, and a manifest file defining app permissions and components.

Uploaded by

Bhushan Karguppi
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)
14 views5 pages

29

The document describes code for an Android application that allows sending and receiving SMS messages. It includes XML layout files for the user interface, Java code for the main activity class, and a manifest file defining app permissions and components.

Uploaded by

Bhushan Karguppi
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

1)->

 Activity_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

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

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

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

<TextView
android:id="@+id/tvReceivedSMS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnSendSMS"
android:layout_marginTop="16dp"
android:text="Received SMS will be displayed here" />
</RelativeLayout>

 MainActivity.java

package com.example.practical29;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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 RECEIVE_SMS_PERMISSION_REQUEST_CODE = 1;


private static final int SEND_SMS_PERMISSION_REQUEST_CODE = 2;
private EditText txtPhoneNum, txtMessage;
private Button btnSendSMS;
private TextView tvReceivedSMS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtPhoneNum = findViewById(R.id.txtPhoneNum);
txtMessage = findViewById(R.id.txtMessage);
btnSendSMS = findViewById(R.id.btnSendSMS);
tvReceivedSMS = findViewById(R.id.tvReceivedSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMS();
}
});
// Register the SMS receiver
registerReceiver(smsReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
// Request SMS permission if not granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECEIVE_SMS},
RECEIVE_SMS_PERMISSION_REQUEST_CODE);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
SEND_SMS_PERMISSION_REQUEST_CODE);
} }
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(smsReceiver);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==RECEIVE_SMS_PERMISSION_REQUEST_CODE ||
requestCode==SEND_SMS_PERMISSION_REQUEST_CODE){
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_LONG).show();
}
} }

// Send SMS method


private void sendSMS() {
String phoneNo = txtPhoneNum.getText().toString();
String msg = txtMessage.getText().toString();

if (!phoneNo.isEmpty() && !msg.isEmpty()) {


SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("tel:"+ phoneNo, null, msg, null, null);
Toast.makeText(this, "SMS sent successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Please enter phone number and Message",
Toast.LENGTH_SHORT).show();
} }
// SMS receiver
private final BroadcastReceiver smsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null && pdus.length > 0) {
// Get the first PDU (assuming there's at least one)
byte[] pdu = (byte[]) pdus[0];
String format = bundle.getString("format");

// Create a message from the PDU


SmsMessage message = SmsMessage.createFromPdu(pdu, format);
// Extract sender's phone number and message body
String sender = message.getOriginatingAddress();
String messageBody = message.getMessageBody();

// Display received SMS


String smsMessage =tvReceivedSMS.getText()+ "\nReceived SMS: " + messageBody + "
from " + sender;
tvReceivedSMS.setText(smsMessage);
}
}
} };}

 Manifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-feature
android:name="android.hardware.telephony"
android:required="true" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Practical29"
tools:targetApi="31">
<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