0% found this document useful (0 votes)
28 views4 pages

Mad PR 29

The document contains code for an Android application that allows users to send SMS messages. It includes an XML layout file with two EditText fields for phone number and message input, and a Button to send the SMS. The Java code handles SMS sending functionality, including permission requests and error handling for cases where no SMS app is available.

Uploaded by

arqamqazi549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

Mad PR 29

The document contains code for an Android application that allows users to send SMS messages. It includes an XML layout file with two EditText fields for phone number and message input, and a Button to send the SMS. The Java code handles SMS sending functionality, including permission requests and error handling for cases where no SMS app is available.

Uploaded by

arqamqazi549
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical No.

29
Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="@drawable/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="MainActivity">

<EditText
android:layout_width="300dp"
android:layout_height="48dp"
android:id="@+id/editText"
android:hint="Enter Phone Number"
android:phoneNumber="true"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="300dp"
android:layout_height="48dp"
android:id="@+id/editText2"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:layout_alignRight="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton"
android:hint="Enter SMS" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Sms"
android:id="@+id/btnSendSMS"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
</LinearLayout>

MainActivity.java
package com.rahman.myapplication;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.Manifest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {


private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ;
Button sendBtn;
EditText txtphoneNo;
EditText txtMessage;
String phoneNo;
String message;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Objects.requireNonNull(getSupportActionBar()).setTitle("SMS Example");

sendBtn = (Button) findViewById(R.id.btnSendSMS);


txtphoneNo = (EditText) findViewById(R.id.editText);
txtMessage = (EditText) findViewById(R.id.editText2);

sendBtn.setOnClickListener(v-> {
sendSMS();
});
}
protected void sendSMS() {
Log.i("Send SMS", "");
// Get the phone number and message from the EditTexts
String phoneNumber = txtphoneNo.getText().toString();
String message = txtMessage.getText().toString();

// Create an Intent to send an SMS


Intent smsIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" +
phoneNumber));
smsIntent.putExtra("sms_body", message);

try {
// Start the SMS activity
startActivity(smsIntent);
Log.i("Finished sending SMS...", "");
} catch (android.content.ActivityNotFoundException ex) {
// Handle the error if no SMS app is found
Log.d("Error", "this is error: " + ex.toString());
Toast.makeText(MainActivity.this, "SMS failed, please try again later.",
Toast.LENGTH_SHORT).show();
}
}
// for permission
void sendSMS2() {
phoneNo = txtphoneNo.getText().toString();
message = txtMessage.getText().toString();

if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) !=
PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[]
grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
return;
}
}
}
}
}
Output:

You might also like