Practical No: 29 - SMS Sending Program
Manifest File:
<uses-feature android:name="android.hardware.telephony"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:ems="10"
android:inputType="text"
android:hint="Phone Number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="Message" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java File:
package com.example.sms_send;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
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;
public class MainActivity extends AppCompatActivity {
SmsManager sm;
Button send;
EditText msg, phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = findViewById(R.id.send);
msg = findViewById(R.id.sms);
phone = findViewById(R.id.phone);
requestMessage();
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermission()) {
sendSMS();
} else {
requestMessage();
}
}
});
}
public void requestMessage() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 0);
}
public void sendSMS() {
try {
String PhoneText = phone.getText().toString();
String MsgTxt = msg.getText().toString();
sm = SmsManager.getDefault();
sm.sendTextMessage(PhoneText, null, MsgTxt, null, null);
Toast.makeText(MainActivity.this, "Message sent", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Message not sent", Toast.LENGTH_SHORT).show();
}
}
public boolean checkPermission() {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) ==
PackageManager.PERMISSION_GRANTED;
}
}
--------------------------------------
Practical No: 30 - Email Sending Program
XML Layout:
<?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"
tools:context=".MainActivity">
<EditText android:id="@+id/toEditText" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="To" />
<EditText android:id="@+id/ccEditText" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="CC" />
<EditText android:id="@+id/bccEditText" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="BCC" />
<EditText android:id="@+id/subjectEditText" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Subject" />
<EditText android:id="@+id/bodyEditText" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Body" android:lines="5" android:minLines="3"
/>
<Button android:id="@+id/sendButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Send Email" />
</RelativeLayout>
Java File:
package com.example.prg30;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText toEditText, ccEditText, bccEditText, subjectEditText, bodyEditText;
Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toEditText = findViewById(R.id.toEditText);
ccEditText = findViewById(R.id.ccEditText);
bccEditText = findViewById(R.id.bccEditText);
subjectEditText = findViewById(R.id.subjectEditText);
bodyEditText = findViewById(R.id.bodyEditText);
sendButton = findViewById(R.id.sendButton);
sendButton.setOnClickListener(v -> {
String to = toEditText.getText().toString();
String cc = ccEditText.getText().toString();
String bcc = bccEditText.getText().toString();
String subject = subjectEditText.getText().toString();
String body = bodyEditText.getText().toString();
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
if (!cc.isEmpty()) {
emailIntent.putExtra(Intent.EXTRA_CC, new String[]{cc});
}
if (!bcc.isEmpty()) {
emailIntent.putExtra(Intent.EXTRA_BCC, new String[]{bcc});
}
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There are no email clients installed.",
Toast.LENGTH_SHORT).show();
}
});
}
}