MADEX3A
MADEX3A
EX NO : 3A
SENDING SMS WITH NOTIFICATION
DATE: 19/8/24
AIM:
To develop an application to send SMS with toast notification using android studio.
PROCEDURE:
Step 1: Open Android Studio and create a new project with an empty activity
template.
Step 2: Add permissions in `AndroidManifest.xml` for sending SMS.
Step 3: Modify `activity_main.xml` to include an `EditText` for the phone number,
another `EditText` for the message, and a `Button` to send the SMS.
Step 4: Update `build.gradle` if necessary to include any SMS-related dependencies.
Step 5: In `MainActivity.java`, set up variables for the `EditText` fields and the
`Button`.
Step 6: Implement the logic to send an SMS using `SmsManager` and display a
`Toast` notification for success or failure.
Step 7: Add runtime permission checks for SMS sending in the main activity.
Step 8: Test the application on a device to verify SMS functionality and toast
notifications.
PROGRAM:
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity"
<EditText android:id="@+id/editTextNumber"
android:layout_width="wrap_content"
61781922106041
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="71dp"
android:layout_marginTop="101dp"
android:ems="10"
android:hint="Number"
android:inputType="number" />
<EditText
android:id="@+id/editTextNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="75dp"
android:layout_marginTop="196dp"
android:ems="10"
android:hint="Message"
android:inputType="text" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="141dp"
android:layout_marginTop="304dp"
android:text="SEND" />
</RelativeLayout>
61781922106041
MainActivity.java
package com.example.sendmessage;
import androidx.appcompat.app.AppCompatActivity;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText e1 = (EditText) findViewById(R.id.editTextNumber);
EditText e2 = (EditText) findViewById(R.id.editTextNumber2);
Button b = (Button) findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String a = e1.getText().toString();
String b = e2.getText().toString(); try {
SmsManager s = SmsManager.getDefault();
s.sendTextMessage(a,null,b,null,null);
Toast.makeText(getApplicationContext() ,"Message send successfully",
Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), "Message not send successfully",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
61781922106041
OUTPUT:
RESULT:
Thus, the program to send sms with toast notification in android has been created
successfully using android studio