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

Send & Receive SMS Full Code

The document describes code for an Android app that allows sending and receiving SMS messages. It includes XML layout files defining buttons, Java code implementing click listeners for the buttons and methods for sending and receiving SMS, and permissions in the app manifest.

Uploaded by

ghanshampawar25
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)
17 views3 pages

Send & Receive SMS Full Code

The document describes code for an Android app that allows sending and receiving SMS messages. It includes XML layout files defining buttons, Java code implementing click listeners for the buttons and methods for sending and receiving SMS, and permissions in the app manifest.

Uploaded by

ghanshampawar25
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

Xml file -:

<!-- activity_main.xml -->


<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">

<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>

<Button
android:id="@+id/btnReceive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Receive SMS"
android:layout_below="@id/btnSend"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>

</RelativeLayout>

Mainactivity.java

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_SEND_SMS_PERMISSION = 1;


private static final String SMS_RECEIVED_ACTION =
"android.provider.Telephony.SMS_RECEIVED";

private Button btnSend;


private Button btnReceive;
private BroadcastReceiver smsReceiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnSend = findViewById(R.id.btnSend);
btnReceive = findViewById(R.id.btnReceive);

btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new
String[]{Manifest.permission.SEND_SMS}, REQUEST_SEND_SMS_PERMISSION);
} else {
sendSMS();
}
}
});

btnReceive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
receiveSMS();
}
});
}

private void sendSMS() {


String phoneNumber = "1234567890"; // Replace with desired recipient's phone number
String message = "Hello, this is a test message!";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(this, "SMS sent", Toast.LENGTH_SHORT).show();
}

private void receiveSMS() {


smsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals(SMS_RECEIVED_ACTION)) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null) {
for (Object pdu : pdus) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
String sender = smsMessage.getOriginatingAddress();
String messageBody = smsMessage.getMessageBody();
Toast.makeText(MainActivity.this, "SMS received from " + sender + ": " +
messageBody, Toast.LENGTH_LONG).show();
}
}
}
}
}
};

IntentFilter intentFilter = new IntentFilter(SMS_RECEIVED_ACTION);


registerReceiver(smsReceiver, intentFilter);
Toast.makeText(this, "Listening for incoming SMS", Toast.LENGTH_SHORT).show();
}

@Override
protected void onDestroy() {
super.onDestroy();
if (smsReceiver != null) {
unregisterReceiver(smsReceiver);
}
}
}

Manifest file

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

You might also like