Download as pdf or txt
Download as pdf or txt
You are on page 1of 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