0% found this document useful (0 votes)
2 views2 pages

Sms (Send&Receive)

The document outlines the development of an SMS application in Java for Android, including necessary permissions in the AndroidManifest.xml file. It provides code for MainActivity.java to send SMS and SmsReceiver.java to receive SMS messages. The application utilizes SmsManager for sending messages and a BroadcastReceiver to handle incoming messages, displaying them via Toast notifications.

Uploaded by

Sakshi Kale
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)
2 views2 pages

Sms (Send&Receive)

The document outlines the development of an SMS application in Java for Android, including necessary permissions in the AndroidManifest.xml file. It provides code for MainActivity.java to send SMS and SmsReceiver.java to receive SMS messages. The application utilizes SmsManager for sending messages and a BroadcastReceiver to handle incoming messages, displaying them via Toast notifications.

Uploaded by

Sakshi Kale
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/ 2

Develop an application to send and receive SMS (Write only Java and permission tag in

manifest file). (M.IMP)

Permissions and tag required 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"/>

<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.myapplication;

import android.Manifest;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {


EditText et1,et2;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
et1=(EditText) findViewById(R.id.et1);
et2=(EditText) findViewById(R.id.et2);
b1=(Button) findViewById(R.id.b1);

ActivityCompat.requestPermissions(MainActivity.this,new
String[]{Manifest.permission.SEND_SMS},1);

b1.setOnClickListener(new View.OnClickListener() {
@Override

BY VIKAS PATIL
public void onClick(View v) {
String phone=et1.getText().toString();
String message=et2.getText().toString();
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(phone,null,message,null,null);
Toast.makeText(MainActivity.this,"Message sent Successfully",
Toast.LENGTH_LONG).show();
}
});
}
}

SmsReceiver.java
package com.example.myapplication;

import android.content.*;
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) {
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null) {
for (Object pdu : pdus) {
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdu);
String message = msg.getMessageBody();
String sender = msg.getOriginatingAddress();
Toast.makeText(context, "From: " + sender + "\nMessage: " + message,
Toast.LENGTH_LONG).show();
}
}
}
}
}

BY VIKAS PATIL

You might also like