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

Practical NO 29

This document outlines the development of an Android application that can send and receive SMS messages. It includes the XML layout for the user interface, Java code for the main activity handling SMS functionality, and the SMS receiver for processing incoming messages. Additionally, it specifies the necessary permissions in the AndroidManifest.xml file to enable SMS operations.
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 views

Practical NO 29

This document outlines the development of an Android application that can send and receive SMS messages. It includes the XML layout for the user interface, Java code for the main activity handling SMS functionality, and the SMS receiver for processing incoming messages. Additionally, it specifies the necessary permissions in the AndroidManifest.xml file to enable SMS operations.
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/ 5

Practical NO.

29 : Develop a program to (a) Send SMS (b) Receive


SMS.

• activity_main.xml

<RelativeLayout android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/ android:layout_height="wrap_content"
apk/res/android" android:hint="Enter message"
android:inputType="text"
xmlns:tools="http://schemas.android.com/to android:padding="12dp"
ols"
android:layout_width="match_parent" android:layout_below="@id/editTextPhone"
android:layout_height="match_parent" android:layout_marginTop="10dp" />
tools:context=".MainActivity">
<Button
<EditText android:id="@+id/buttonSend"
android:id="@+id/editTextPhone" android:layout_width="wrap_content"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:text="Send SMS"
android:hint="Enter phone number"
android:inputType="phone" android:layout_below="@id/editTextMessag
android:padding="12dp" e"
android:layout_marginTop="50dp" /> android:layout_marginTop="20dp"
android:layout_centerHorizontal="true" />
<EditText </RelativeLayout>
android:id="@+id/editTextMessage"

• MainActivity.java

package com.example.twentynine;
private EditText editTextPhone,
import android.Manifest; editTextMessage;
import android.content.IntentFilter; private Button buttonSend;
import android.content.pm.PackageManager;
import android.os.Bundle; @Override
import android.telephony.SmsManager; protected void onCreate(Bundle
import android.view.View; savedInstanceState) {
import android.widget.Button; super.onCreate(savedInstanceState);
import android.widget.EditText; setContentView(R.layout.activity_main);
import android.widget.Toast;
editTextPhone =
import androidx.annotation.NonNull; findViewById(R.id.editTextPhone);
import editTextMessage =
androidx.appcompat.app.AppCompatActivity; findViewById(R.id.editTextMessage);
import androidx.core.app.ActivityCompat; buttonSend =
import androidx.core.content.ContextCompat; findViewById(R.id.buttonSend);

public class MainActivity extends // Request SMS permissions


AppCompatActivity { requestSMSPermission();

private static final int // Register SMS receiver dynamically


PERMISSION_REQUEST_CODE = 1; SMSReceiver smsReceiver = new
SMSReceiver(); onRequestPermissionsResult(int requestCode,
IntentFilter filter = new @NonNull String[] permissions, @NonNull
IntentFilter("android.provider.Telephony.SM int[] grantResults) {
S_RECEIVED");
registerReceiver(smsReceiver, filter); super.onRequestPermissionsResult(requestCo
de, permissions, grantResults);
// Send SMS on button click if (requestCode ==
buttonSend.setOnClickListener(new PERMISSION_REQUEST_CODE) {
View.OnClickListener() { if (grantResults.length > 0 &&
@Override grantResults[0] ==
public void onClick(View v) { PackageManager.PERMISSION_GRANTED)
String phoneNumber = {
editTextPhone.getText().toString().trim(); Toast.makeText(this, "Permissions
String message = granted!", Toast.LENGTH_SHORT).show();
editTextMessage.getText().toString().trim(); } else {
Toast.makeText(this, "Permissions
if (!phoneNumber.isEmpty() && denied. SMS features won't work.",
!message.isEmpty()) { Toast.LENGTH_SHORT).show();
sendSMS(phoneNumber, }
message); }
} else { }

Toast.makeText(MainActivity.this, "Please // Send SMS


enter phone number and message", private void sendSMS(String
Toast.LENGTH_SHORT).show(); phoneNumber, String message) {
} try {
} SmsManager smsManager =
}); SmsManager.getDefault();
}
smsManager.sendTextMessage(phoneNumber
// Request SMS permissions , null, message, null, null);
private void requestSMSPermission() { Toast.makeText(this, "SMS sent
if successfully!",
(ContextCompat.checkSelfPermission(this, Toast.LENGTH_SHORT).show();
Manifest.permission.SEND_SMS) != } catch (Exception e) {
PackageManager.PERMISSION_GRANTED || Toast.makeText(this, "SMS sending
failed: " + e.getMessage(),
ContextCompat.checkSelfPermission(this, Toast.LENGTH_LONG).show();
Manifest.permission.RECEIVE_SMS) != }
PackageManager.PERMISSION_GRANTED) }
{ }
String username =
ActivityCompat.requestPermissions(this, new usernameEditText.getText().toString().trim();
String[]{ String password =
Manifest.permission.SEND_SMS, passwordEditText.getText().toString().trim();

Manifest.permission.RECEIVE_SMS, // Check if username and password meet


Manifest.permission.READ_SMS the required conditions
}, PERMISSION_REQUEST_CODE); boolean isUsernameValid =
} username.length() >= 3; // Username must be
} at least 3 characters
boolean isPasswordValid =
// Handle permission results password.length() >= 6; // Password must be
@Override at least 6 characters
public void
isPasswordValid);
// Enable button only when both fields }
are valid }

loginButton.setEnabled(isUsernameValid &&

• SMSReceiver.java

package com.example.twentynine; SmsMessage.createFromPdu((byte[]) pdu);


String senderNumber =
import android.content.BroadcastReceiver; smsMessage.getDisplayOriginatingAddress();
import android.content.Context; String messageBody =
import android.content.Intent; smsMessage.getMessageBody();
import android.os.Bundle;
import android.telephony.SmsMessage; // Log the message for debugging
import android.widget.Toast; Log.d("SMSReceiver", "SMS
import android.util.Log; from: " + senderNumber + " Message: " +
messageBody);
public class SMSReceiver extends
BroadcastReceiver { // Show a Toast notification
Toast.makeText(context, "SMS
@Override from: " + senderNumber + "\nMessage: " +
public void onReceive(Context context, messageBody,
Intent intent) { Toast.LENGTH_LONG).show();
Bundle bundle = intent.getExtras(); }
if (bundle != null) { }
Object[] pdus = (Object[]) }
bundle.get("pdus"); }
if (pdus != null) { }
for (Object pdu : pdus) {
SmsMessage smsMessage =

• AndroidManifest.xml

<manifest android:name="android.permission.RE
xmlns:android="http://schemas.android. AD_SMS" />
com/apk/res/android"
<application
package="com.example.twentynine"> android:allowBackup="true"
android:supportsRtl="true"
<!-- Permissions for sending and android:label="SMS App"
receiving SMS -->
<uses-permission android:theme="@style/Theme.AppCo
android:name="android.permission.SE mpat.Light.DarkActionBar">
ND_SMS" />
<uses-permission <!-- MainActivity with explicit
android:name="android.permission.RE android:exported -->
CEIVE_SMS" />
<activity
<uses-permission
android:name=".MainActivity" CEIVE_SMS" />
android:exported="true"> <uses-permission
<intent-filter> android:name="android.permission.RE
<action AD_SMS" />
android:name="android.intent.action.M
AIN" /> <application
<category android:allowBackup="true"
android:name="android.intent.category. android:supportsRtl="true"
LAUNCHER" /> android:label="SMS App"
</intent-filter>
</activity> android:theme="@style/Theme.AppCo
mpat.Light.DarkActionBar">
<!-- Register SMSReceiver with
explicit android:exported --> <!-- MainActivity with explicit
<receiver android:exported -->
android:name=".SMSReceiver" <activity
android:name=".MainActivity"
android:permission="android.permissio android:exported="true">
n.BROADCAST_SMS" <intent-filter>
android:exported="true"> <action
<intent-filter> android:name="android.intent.action.M
<action AIN" />
android:name="android.provider.Teleph <category
ony.SMS_RECEIVED" /> android:name="android.intent.category.
</intent-filter> LAUNCHER" />
</receiver> </intent-filter>
</activity>
</application>
<!-- Register SMSReceiver with
</manifest> <manifest explicit android:exported -->
xmlns:android="http://schemas.android. <receiver
com/apk/res/android" android:name=".SMSReceiver"

package="com.example.twentynine"> android:permission="android.permissio
n.BROADCAST_SMS"
<!-- Permissions for sending and android:exported="true">
receiving SMS --> <intent-filter>
<uses-permission <action
android:name="android.permission.SE android:name="android.provider.Teleph
ND_SMS" /> ony.SMS_RECEIVED" />
<uses-permission </intent-filter>
android:name="android.permission.RE </receiver>
</application>

</manifest>

Output:

You might also like