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

Exp 29

The document describes how to build an Android application to send and receive SMS messages. It includes XML layouts for the user interface, permissions needed in the manifest, and Java code for the main activity and SMS receiver classes.

Uploaded by

HARSH MAGHNANI
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)
22 views

Exp 29

The document describes how to build an Android application to send and receive SMS messages. It includes XML layouts for the user interface, permissions needed in the manifest, and Java code for the main activity and SMS receiver classes.

Uploaded by

HARSH MAGHNANI
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/ 4

Exp29: Write a program to send and receive SMS, make use of following GUI

activitity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android SMS"
android:textSize="20dp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textSize="18dp"
android:layout_below="@+id/heading"
/>
<EditText
android:id="@+id/etphno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1"
/>
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SMS Message"
android:textSize="18dp"
android:layout_below="@+id/etphno"
/>
<EditText
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textview2"
/>
<Button
android:id="@+id/sendmsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send SMS"
android:layout_below="@+id/msg"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="~Developed By Harsh.Maghnani (Roll no:06)"
android:textColor="#0000FF"
android:textSize="16dp"
android:layout_below="@+id/sendmsg"
/>

</RelativeLayout>

Add below in AndroidManifest.xml:


<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>

Inside application tag:


<receiver
android:name=".SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

MainActivity.java:
package com.example.exp29;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
SmsReceiver sms= new SmsReceiver();
EditText et1,et2;
Button b1;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=findViewById(R.id.etphno);
et2=findViewById(R.id.msg);
b1=findViewById(R.id.sendmsg);

if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.
SEND_SMS)!=
PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this,new
String[]{Manifest.permission.SEND_SMS},100);
}
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String phno= et1.getText().toString();
String msg=et2.getText().toString();
SmsManager smsManager= SmsManager.getDefault();
smsManager.sendTextMessage(phno,null,msg,null,null);
Toast.makeText(MainActivity.this,"Sms sent
successfully",
Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Toast.makeText(MainActivity.this,"Sms failed to send...
try again",
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
IntentFilter filter=new
IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(sms,filter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(sms);
}
}
SmsReciever.java:
package com.example.exp29;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
// Retrieve the SMS Messages received
Object[] sms = (Object[]) bundle.get("pdus");
// For every SMS message received
for (int i=0; i < sms.length; i++) {
// Convert Object array
SmsMessage smsMessage =
SmsMessage.createFromPdu((byte[]) sms[i]);
String phone = smsMessage.getOriginatingAddress();
String message =
smsMessage.getMessageBody().toString();
Toast.makeText(context, "Received from "+ phone + ": "
+ message, Toast.LENGTH_SHORT).show();
}
}
}
}
Output:

You might also like