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

Exp 29

The document outlines a program for sending and receiving SMS messages using a GUI in Android. It includes XML layout for the user interface and Java code for the main activity, SMS sending functionality, and SMS receiving through a broadcast receiver. The program handles permissions for SMS and updates the UI with received messages.
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)
8 views4 pages

Exp 29

The document outlines a program for sending and receiving SMS messages using a GUI in Android. It includes XML layout for the user interface and Java code for the main activity, SMS sending functionality, and SMS receiving through a broadcast receiver. The program handles permissions for SMS and updates the UI with received messages.
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

Experiment 29

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


GUI.
Activity_main.xml android:layout_height="wrap_content" />
<?xml version="1.0" encoding="utf-8"?> </LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com MainActivity.java
/apk/res/android"
package com.example.exp_23;
xmlns:app="http://schemas.android.com/ap import androidx.annotation.NonNull;
k/res-auto" import
androidx.appcompat.app.AppCompatAc
xmlns:tools="http://schemas.android.com/to
tivity;
ols"
android:orientation="vertical"
import android.Manifest;
android:padding="16dp" import
android:layout_width="match_parent" android.content.pm.PackageManager;
android:layout_height="match_parent"> import android.os.Bundle;
<EditText import android.telephony.SmsManager;
android:id="@+id/etPhoneNumber" import android.view.View;
android:hint="Phone Number" import android.widget.Button;
android:inputType="phone" import android.widget.EditText;
android:layout_width="match_parent" import android.widget.ListView;
import android.widget.Toast;
android:layout_height="wrap_content" />
import java.util.ArrayList;
<EditText
public class MainActivity extends
android:id="@+id/etMessage" AppCompatActivity {
android:hint="SMS Message" private static final int
android:layout_width="match_parent" SMS_PERMISSION_CODE = 101;
private EditText etPhoneNumber,
android:layout_height="wrap_content" /> etMessage;
private Button btnSend;
<Button private ListView lvMessages;
android:id="@+id/btnSend" private ArrayList<String> messagesList;
android:text="Send SMS" private MessageAdapter adapter;
android:layout_width="match_parent"
@Override
android:layout_height="wrap_content" />
protected void onCreate(Bundle
savedInstanceState) {
<ListView
android:id="@+id/lvMessages" super.onCreate(savedInstanceState);
android:layout_width="match_parent"
setContentView(R.layout.activity_main);
etPhoneNumber = adapter.notifyDataSetChanged();
findViewById(R.id.etPhoneNumber); });
etMessage = }
findViewById(R.id.etMessage);
btnSend = // Method to send SMS
findViewById(R.id.btnSend); private void sendSMS() {
lvMessages = String phoneNumber =
findViewById(R.id.lvMessages); etPhoneNumber.getText().toString().trim
();
messagesList = new ArrayList<>(); String message =
adapter = new etMessage.getText().toString().trim();
MessageAdapter(this, messagesList);
lvMessages.setAdapter(adapter); if (phoneNumber.isEmpty() ||
message.isEmpty()) {
// Request Permissions Toast.makeText(this, "Please fill
if all fields",
(checkSelfPermission(Manifest.permissi Toast.LENGTH_SHORT).show();
on.SEND_SMS) != return;
PackageManager.PERMISSION_GRAN }
TED ||
try {
checkSelfPermission(Manifest.permissio SmsManager smsManager =
n.RECEIVE_SMS) != SmsManager.getDefault();
PackageManager.PERMISSION_GRAN
TED) { smsManager.sendTextMessage(phone
Number, null, message, null, null);
requestPermissions(new String[] Toast.makeText(this, "SMS Sent
{ Successfully!",
Toast.LENGTH_SHORT).show();
Manifest.permission.SEND_SMS, } catch (Exception e) {
Toast.makeText(this, "Failed to
Manifest.permission.RECEIVE_SMS, send SMS",
Toast.LENGTH_SHORT).show();
Manifest.permission.READ_SMS e.printStackTrace();
}, SMS_PERMISSION_CODE); }
} }
// Send SMS Button Click Listener // Handle Permissions Result
btnSend.setOnClickListener(v -> @Override
sendSMS()); public void
onRequestPermissionsResult(int
// Bind SMS Receiver requestCode, @NonNull String[]
SmsReceiver.bindListener((from, permissions, @NonNull int[]
message) -> { grantResults) {
messagesList.add("SMS From: "
+ from + "\n" + message); super.onRequestPermissionsResult(req
uestCode, permissions, grantResults); Object[] pdus = (Object[])
bundle.get("pdus");
if (requestCode == if (pdus != null) {
SMS_PERMISSION_CODE) { for (Object pdu : pdus) {
if (grantResults.length > 0 && SmsMessage smsMessage
grantResults[0] == = SmsMessage.createFromPdu((byte[])
PackageManager.PERMISSION_GRAN pdu);
TED) { String sender =
Toast.makeText(this, smsMessage.getDisplayOriginatingAddr
"Permissions Granted", ess();
Toast.LENGTH_SHORT).show(); String message =
} else { smsMessage.getMessageBody();
Toast.makeText(this,
"Permissions Denied", Toast.makeText(context,
Toast.LENGTH_SHORT).show(); "SMS Received from: " + sender,
} Toast.LENGTH_SHORT).show();
}
} if (listener != null) {
}
listener.onSmsReceived(sender,
SMSRECEIVER.JAVA message);
}
package com.example.exp_23; }
}
import }
android.content.BroadcastReceiver; }
import android.content.Context; }
import android.content.Intent;
import android.os.Bundle; SMSLISENTER.JAVA
import android.telephony.SmsMessage;
import android.widget.Toast; package com.example.exp_23;
import android.content.Context;
public class SmsReceiver extends import android.view.LayoutInflater;
BroadcastReceiver { import android.view.View;
import android.view.ViewGroup;
private static SmsListener listener; import android.widget.ArrayAdapter;
import android.widget.TextView;
public static void
bindListener(SmsListener smsListener) { import java.util.ArrayList;
listener = smsListener;
} public class MessageAdapter extends
ArrayAdapter<String> {
@Override
public void onReceive(Context public MessageAdapter(Context
context, Intent intent) { context, ArrayList<String> messages) {
Bundle bundle = intent.getExtras(); super(context, 0, messages);
if (bundle != null) { }
@Override
public View getView(int position, View
convertView, ViewGroup parent) {
if (convertView == null) {
convertView =
LayoutInflater.from(getContext()).inflate(
android.R.layout.simple_list_item_1,
parent, false);
}

String message =
getItem(position);
TextView textView =
convertView.findViewById(android.R.id.t
ext1);
textView.setText(message);

return convertView;
}
}

Output

You might also like