0% found this document useful (0 votes)
7 views6 pages

Aad 6

The document outlines the design of an Android application that sends SMS messages using Intent. It includes the Java code for the MainActivity, which handles SMS permissions and sending messages, as well as the XML layout for the user interface. Additionally, it provides the AndroidManifest.xml configuration required for the application to function properly, including the necessary permissions.

Uploaded by

229x1a33b3
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)
7 views6 pages

Aad 6

The document outlines the design of an Android application that sends SMS messages using Intent. It includes the Java code for the MainActivity, which handles SMS permissions and sending messages, as well as the XML layout for the user interface. Additionally, it provides the AndroidManifest.xml configuration required for the application to function properly, including the necessary permissions.

Uploaded by

229x1a33b3
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/ 6

ExpNo.

: 6 Title: Design an android application Send SMS using Intent Date:

Aim: Design an android application Send SMS using Intent

MainActivity.java:
package com.example.sai;

import android.Manifest;

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;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;

import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

private static final int PERMISSION_REQUEST_SEND_SMS = 1;

private EditText phoneNumberEditText, messageEditText;

private Button sendButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

phoneNumberEditText = findViewById(R.id.editTextPhoneNumber);

messageEditText = findViewById(R.id.editTextMessage);

sendButton = findViewById(R.id.buttonSend);

sendButton.setOnClickListener(new View.OnClickListener() {

229X1A33B3
@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},

PERMISSION_REQUEST_SEND_SMS);

} else {

sendSMS();

});

private void sendSMS() {

String phoneNumber = phoneNumberEditText.getText().toString();

String message = messageEditText.getText().toString();

try {

SmsManager smsManager = SmsManager.getDefault();

smsManager.sendTextMessage(phoneNumber, null, message, null, null);

Toast.makeText(this, "SMS sent successfully", Toast.LENGTH_SHORT).show();

} catch (Exception e) {

Toast.makeText(this, "Failed to send SMS", Toast.LENGTH_SHORT).show();

e.printStackTrace();

@Override

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]


grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == PERMISSION_REQUEST_SEND_SMS) {

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

229X1A33B3
sendSMS();

} else {

Toast.makeText(this, "Permission denied to send SMS", Toast.LENGTH_SHORT).show();

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

android:padding="16dp"

tools:context=".MainActivity">

<EditText

android:id="@+id/editTextPhoneNumber"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Phone Number"

android:inputType="phone" />

<EditText

android:id="@+id/editTextMessage"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/editTextPhoneNumber"

android:layout_marginTop="16dp"

android:hint="Message"

android:inputType="textMultiLine"

android:minLines="3" />

229X1A33B3
<Button

android:id="@+id/buttonSend"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/editTextMessage"

android:layout_marginTop="16dp"

android:text="Send" />

</RelativeLayout>

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

package="com.example.sai">

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

<application

android:allowBackup="true"

android:dataExtractionRules="@xml/data_extraction_rules"

android:fullBackupContent="@xml/backup_rules"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/Theme.Sai"

tools:targetApi="31">

<activity

android:name=".MainActivity"

android:exported="true"

229X1A33B3
android:label="@string/app_name"

android:theme="@style/Theme.Sai">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

Output :

229X1A33B3
Output :

229X1A33B3

You might also like