0% found this document useful (0 votes)
33 views3 pages

Practical 29

The document describes an Android SMS app with an XML layout file defining the user interface, a MainActivity class for handling button clicks and sending SMS messages, and a MyReceiver class for receiving incoming SMS messages.

Uploaded by

l.i.a.g.ge.i.29o
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)
33 views3 pages

Practical 29

The document describes an Android SMS app with an XML layout file defining the user interface, a MainActivity class for handling button clicks and sending SMS messages, and a MyReceiver class for receiving incoming SMS messages.

Uploaded by

l.i.a.g.ge.i.29o
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 29

 Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SMS APP"
android:textSize="40dp"
android:gravity="center"/>

<EditText
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/smstxt"
android:hint="Enter The Message"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND SMS"
android:gravity="center"
android:id="@+id/smssend"
android:layout_gravity="center"/>

<TextView
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="SMS"
android:id="@+id/smsget"
android:textSize="30dp"
android:gravity="center"/>

</LinearLayout>

 MainActivity

package com.example.sms_app;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
MyReceiver myReceiver = new MyReceiver();
Button smssend;
EditText smstxt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smstxt = findViewById(R.id.smstxt);
smssend = findViewById(R.id.smssend);

smssend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SmsManager sobj = SmsManager.getDefault();
try {
sobj.sendTextMessage("+917876787678", null, smstxt.getText().toString(), null, null);
Toast.makeText(MainActivity.this, "MESSAGE SENT", Toast.LENGTH_LONG).show();

} catch (Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
Log.d("error", "onClick: " + e.getMessage());
}
}
});
}
}

 MyReceiver

package com.example.sms_app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver


{
TextView smstxt;
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
Object[] smsobj = (Object[]) bundle.get("pdus");
for (Object obj : smsobj)
{
SmsMessage msg = SmsMessage.createFromPdu((byte[]) obj);
String mob = msg.getDisplayOriginatingAddress();
String txt = msg.getDisplayMessageBody();

Log.d("key","mobile"+mob+"text"+txt);
Toast.makeText(context.getApplicationContext(),"mob"+mob+"text"+txt,Toast.LENGTH_LONG).show();
}
}
}

You might also like