0% found this document useful (0 votes)
517 views

Practical No 29

This document contains code for a Java Android application that allows sending SMS messages and receiving SMS messages. It includes code for a main activity class with methods to send messages via SMSManager, and a broadcast receiver to receive incoming SMS messages and display them. The XML layout code defines the user interface with fields for phone number, message, and buttons to send and refresh.
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)
517 views

Practical No 29

This document contains code for a Java Android application that allows sending SMS messages and receiving SMS messages. It includes code for a main activity class with methods to send messages via SMSManager, and a broadcast receiver to receive incoming SMS messages and display them. The XML layout code defines the user interface with fields for phone number, message, and buttons to send and refresh.
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/ 3

Practical No.

29

JAVA Code ( MainActivity.java)

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.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

private EditText phoneNumberEditText;


private EditText messageEditText;
private Button sendButton;
private TextView messagesTextView;

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

phoneNumberEditText = findViewById(R.id.phone_number_edit_text);
messageEditText = findViewById(R.id.message_edit_text);
sendButton = findViewById(R.id.send_button);
messagesTextView = findViewById(R.id.messages_text_view);

sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phoneNumber = phoneNumberEditText.getText().toString();
String message = messageEditText.getText().toString();

if (phoneNumber.isEmpty() || message.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter phone number and message",
Toast.LENGTH_SHORT).show();
} else {
sendMessage(phoneNumber, message);
messageEditText.setText("");
}
}
});

registerReceiver(receiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));


}

private void sendMessage(String phoneNumber, String message) {


SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(this, "Message sent to " + phoneNumber, Toast.LENGTH_SHORT).show();
}

private BroadcastReceiver receiver = 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) {
for (Object pdu : pdus) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
String message = smsMessage.getMessageBody();
String sender = smsMessage.getOriginatingAddress();
messagesTextView.append(sender + ": " + message + "\n");
}
}
}
}
};
}

XML Code (activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone number"/>

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

<Button
android:id="@+id/buttonSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send"/>

<Button
android:id="@+id/buttonRefresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Refresh"/>

</LinearLayout>

Output:

You might also like