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

SMSProgram

The document outlines an Android application that allows users to send and receive SMS messages. It includes necessary permissions for SMS operations, layout design for user input, and Java code for sending messages and receiving incoming SMS. The application features an EditText for phone numbers and messages, a button to send SMS, and a BroadcastReceiver to handle incoming messages.

Uploaded by

dasmitag147
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SMSProgram

The document outlines an Android application that allows users to send and receive SMS messages. It includes necessary permissions for SMS operations, layout design for user input, and Java code for sending messages and receiving incoming SMS. The application features an EditText for phone numbers and messages, a button to send SMS, and a BroadcastReceiver to handle incoming messages.

Uploaded by

dasmitag147
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SMS-SEND and RECEIVE

Permissions-------
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
Activity.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/phonenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Phone Number"
android:inputType="phone" />

<EditText
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message"
android:inputType="textMultiLine" />

<Button
android:id="@+id/sendSMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"
android:layout_gravity="center" />
</LinearLayout>
MainActivity.java------------------
package com.example.smsmessanger;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
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 {


EditText phoneNumber, message;
Button sendSMS;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

phoneNumber = findViewById(R.id.phonenumber);
message = findViewById(R.id.message);
sendSMS = findViewById(R.id.sendSMS);

sendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

sendSMS();
}
});
}

private void sendSMS() {


String phone = phoneNumber.getText().toString().trim();
String msg = message.getText().toString().trim();

if (!phone.isEmpty() && !msg.isEmpty()) {


SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phone, null, msg, null, null);
Toast.makeText(this, "SMS Sent!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Enter phone number and message",
Toast.LENGTH_SHORT).show();
}
}
}

SMSReciever.java----------------------------------------
package com.example.smsmessanger;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
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");
String format = bundle.getString("format");
// For every SMS message received
for (int i=0; i < sms.length; i++)
{
// Convert Object array
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])
sms[i], format);
String sender = smsMessage.getDisplayOriginatingAddress();
String message = smsMessage.getMessageBody();
Toast.makeText(context, "SMS from: " + sender + "\nMessage: " +
message,
Toast.LENGTH_LONG).show();
}
}
}
}

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

You might also like