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

Practical No 29

This document contains code for an Android SMS app that allows sending and receiving SMS messages. The activity_main.xml layout file contains UI elements like edit texts for the phone number and message, and text views to display the SMS inbox. The MainActivity handles sending SMS via intents and registers a broadcast receiver to update the inbox on new messages. The smsReceiver class extends BroadcastReceiver and handles receiving new SMS, extracting the sender and message, then broadcasting an intent to update the inbox in MainActivity.

Uploaded by

atharvabutte03
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)
46 views2 pages

Practical No 29

This document contains code for an Android SMS app that allows sending and receiving SMS messages. The activity_main.xml layout file contains UI elements like edit texts for the phone number and message, and text views to display the SMS inbox. The MainActivity handles sending SMS via intents and registers a broadcast receiver to update the inbox on new messages. The smsReceiver class extends BroadcastReceiver and handles receiving new SMS, extracting the sender and message, then broadcasting an intent to update the inbox in MainActivity.

Uploaded by

atharvabutte03
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 No 29

Q1.
activity_main.xml
<?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">
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="162dp"
android:layout_height="48dp"
android:inputType="textPersonName"
android:text="Atharva Butte"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<LinearLayout
android:layout_width="298dp"
android:layout_height="505dp"
android:layout_marginStart="56dp"
android:layout_marginTop="128dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/pno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:inputType="text" />
<EditText
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message"
android:inputType="text" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="sendMsg"
android:text="Send" />
<Space
android:layout_width="match_parent"
android:layout_height="80dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SMS Inbox"
android:textAlignment="center" />
<TextView
android:id="@+id/sms_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practicla29;
import androidx.appcompat.app.AppCompatActivity;
import android.app.PendingIntent;
import android.content.*;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
TextView sms_list;
private BroadcastReceiver receiver ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sms_list = findViewById(R.id.sms_list);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
((TextView) findViewById(R.id.sms_list)).setText
("SMS From: "+intent.getExtras().getString("mob")+
"\n"+intent.getExtras().getString("msg")+"\n");
}
};
IntentFilter filter = new IntentFilter("SMS_RECEIVED_ACTION");
registerReceiver(receiver, filter);
}

public void sendMsg(View view) {


SmsManager smsManager = SmsManager.getDefault();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(getApplicationContext(), 6, intent, 0);
smsManager.sendTextMessage(((EditText)
findViewById(R.id.pno)).getText().toString(), null, ((EditText)
findViewById(R.id.msg)).getText().toString(), pendingIntent, null);
Toast.makeText(this, "Message Sent !", Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(receiver);
}
}

smsReciver.java
package com.example.practicla29;
import android.content.*;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class smsReciver extends BroadcastReceiver {
private static final String TAG = "MainActivity";
@Override
public void onReceive(Context context, Intent intent) {
String msg="",mob_no="";
Bundle bundle = intent.getExtras();
if (bundle != null){
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null){
for (Object obj : pdus){
SmsMessage sms = SmsMessage.createFromPdu((byte[]) obj);
msg = sms.getDisplayMessageBody();
mob_no = sms.getDisplayOriginatingAddress();
} } }
Intent smsint = new Intent();
smsint.setAction("SMS_RECEIVED_ACTION");
Bundle data_extras = new Bundle();
data_extras.putString("mob",mob_no);
data_extras.putString("msg",msg);
smsint.putExtras(data_extras);
context.sendBroadcast(smsint);
}
}

You might also like