0% found this document useful (0 votes)
13 views2 pages

Practical 30

The document outlines the implementation of an SMS application in Android, including the layout for sending and receiving SMS messages. It details the use of BroadcastReceiver to handle incoming SMS, and the necessary permissions for sending SMS. Additionally, it includes XML layouts for user interface components such as EditText and Button for user input and interaction.

Uploaded by

janhavirhude
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)
13 views2 pages

Practical 30

The document outlines the implementation of an SMS application in Android, including the layout for sending and receiving SMS messages. It details the use of BroadcastReceiver to handle incoming SMS, and the necessary permissions for sending SMS. Additionally, it includes XML layouts for user interface components such as EditText and Button for user input and interaction.

Uploaded by

janhavirhude
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/ 2

Practical 30 Object[] pdus = (Object[]) bundle.

get("pdus");
if (pdus != null) {
for (Object pdu : pdus) {
SmsMessage smsMessage =
SmsMessage.createFromPdu((byte[]) pdu);
Acidity_main.xml
String sender =
<?xml version="1.0" encoding="utf-8"?> smsMessage.getDisplayOriginatingAddress();
<LinearLayout String messageBody =
xmlns:android="http://schemas.android.com/apk/res/andr smsMessage.getMessageBody();
oid"
xmlns:tools="http://schemas.android.com/tools" Toast.makeText(context, "SMS From: " +
android:layout_width="match_parent" sender + "\n" + messageBody,
android:layout_height="match_parent" Toast.LENGTH_LONG).show();
android:orientation="vertical" }
android:padding="20dp" }
android:gravity="center"> }
}
<EditText }
android:id="@+id/editTextPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content" acitivity_receive_sms.xml
android:hint="Phone Number"
android:inputType="phone"
tools:ignore="TouchTargetSizeCheck" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<EditText
xmlns:android="http://schemas.android.com/apk/res/andr
android:id="@+id/editTextMessage"
oid"
android:layout_width="match_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:hint="SMS Message"
android:padding="20dp"
tools:ignore="TouchTargetSizeCheck" />
android:orientation="vertical">

<Button
<TextView
android:id="@+id/buttonSend"
android:id="@+id/textViewInbox"
android:layout_width="match_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"/>
android:text="SMS Inbox"
</LinearLayout>
android:textStyle="bold"
android:textSize="18sp"
android:gravity="center"/>

smsReceiver.java <ListView
android:id="@+id/listViewSms"
package com.example.smsapp;
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
import android.content.BroadcastReceiver;
</LinearLayout>
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage; ReceiveSmsAcivity.java
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver { <?xml version="1.0" encoding="utf-8"?>


@Override <LinearLayout
public void onReceive(Context context, Intent intent) xmlns:android="http://schemas.android.com/apk/res/andr
{ oid"
Bundle bundle = intent.getExtras(); android:layout_width="match_parent"
if (bundle != null) { android:layout_height="match_parent"
android:padding="20dp" Manifest.permission.SEND_SMS)
android:orientation="vertical"> !=
PackageManager.PERMISSION_GRANTED) {
<TextView ActivityCompat.requestPermissions(this, new
android:id="@+id/textViewInbox" String[]{Manifest.permission.SEND_SMS},
android:layout_width="match_parent" SMS_PERMISSION_REQUEST);
android:layout_height="wrap_content" }
android:text="SMS Inbox"
android:textStyle="bold" buttonSend.setOnClickListener(new
android:textSize="18sp" View.OnClickListener() {
android:gravity="center"/> @Override
public void onClick(View v) {
<ListView String phoneNumber =
android:id="@+id/listViewSms" editTextPhoneNumber.getText().toString().trim();
android:layout_width="match_parent" String message =
android:layout_height="wrap_content"/> editTextMessage.getText().toString().trim();
</LinearLayout>
if (phoneNumber.isEmpty() ||
message.isEmpty()) {
Toast.makeText(MainActivity.this, "Please
enter phone number and message",
MainAcitivity.java Toast.LENGTH_SHORT).show();
return;
}
package com.example.smsapp;
try {
import android.Manifest; SmsManager smsManager =
import android.content.pm.PackageManager; SmsManager.getDefault();
import android.os.Bundle;
import android.telephony.SmsManager; smsManager.sendTextMessage(phoneNumber, null,
import android.view.View; message, null, null);
import android.widget.Button; Toast.makeText(MainActivity.this, "SMS
import android.widget.EditText; Sent Successfully!", Toast.LENGTH_SHORT).show();
import android.widget.Toast; } catch (Exception e) {
import androidx.appcompat.app.AppCompatActivity; Toast.makeText(MainActivity.this, "Failed to
import androidx.core.app.ActivityCompat; send SMS!", Toast.LENGTH_SHORT).show();
import androidx.core.content.ContextCompat; e.printStackTrace();
}
public class MainActivity extends AppCompatActivity { }
});
EditText editTextPhoneNumber, editTextMessage; }
Button buttonSend; }
private static final int SMS_PERMISSION_REQUEST
= 101;
Output:-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editTextPhoneNumber =
findViewById(R.id.editTextPhoneNumber);
editTextMessage =
findViewById(R.id.editTextMessage);
buttonSend = findViewById(R.id.buttonSend);

// Request SMS permission


if (ContextCompat.checkSelfPermission(this,

You might also like