0% found this document useful (0 votes)
73 views5 pages

Package Import Import Import Import Import Import Public Class Extends Protected Void Super

The document describes an Android application that allows sending SMS messages using two different methods - using an Intent or using the SMS Manager. It provides code samples for the MainActivity class and XML layout files to build a user interface with fields to enter a phone number and message. It also includes the necessary permissions and intent filters in the AndroidManifest file. The application aims to implement SMS functionality through these two common approaches.

Uploaded by

shafe SP
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)
73 views5 pages

Package Import Import Import Import Import Import Public Class Extends Protected Void Super

The document describes an Android application that allows sending SMS messages using two different methods - using an Intent or using the SMS Manager. It provides code samples for the MainActivity class and XML layout files to build a user interface with fields to enter a phone number and message. It also includes the necessary permissions and intent filters in the AndroidManifest file. The application aims to implement SMS functionality through these two common approaches.

Uploaded by

shafe SP
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/ 5

Name : Pahochiya Mohammadshafe

Enroll No. : 18BECE30537


Aim : Implement an application to send SMS – Message can be sent using 2
methods – using Intent, using SMS Manager.
1. Using Intent

MainActivity.java
package com.example.sendsms;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

Button mButton = (Button) findViewById(R.id.send_sms);


mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.fromParts("sms","987654321", null));
intent.putExtra("sms_body", "Hello Dear...");
startActivity(intent);
}
});
}
}

Activity_main.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">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/send_sms"
android:text="@string/send_sms"/>

</RelativeLayout>

DBHelper.java
<resources>
<string name="app_name">SendSMS</string>

<string name="hello_world">Hello world!</string>


<string name="action_settings">Setting</string>
<string name="title_activity_second">SecondActivity</string>
<string name="main_activity">Main Activity</string>

<string name="send_sms">Click to send SMS</string>


</resources>

Output :
2. Using SMS Manager

MainActivity.java
package com.example.sendsms2;

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

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 {

final int SEND_SMS_PERMISSION_REQUEST_CODE = 1;

EditText number;
EditText message;
Button send;

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

number = findViewById(R.id.inputNumber);
message = findViewById(R.id.inputMessage);
send = findViewById(R.id.buttonSend);

send.setEnabled(false);
if(checkPermission(Manifest.permission.SEND_SMS)){
send.setEnabled(true);
}else{
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
SEND_SMS_PERMISSION_REQUEST_CODE);
}
}

public void onSend(View v){


String phoneNumber = number.getText().toString();
String smsMessage = message.getText().toString();

if(phoneNumber == null || phoneNumber.length() == 0 ||


smsMessage == null || smsMessage.length() == 0){
return;
}

if(checkPermission(Manifest.permission.SEND_SMS)){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, smsMessage, null, null);
Toast.makeText(this, "Message Sent!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}

public boolean checkPermission(String permission){


int check = ContextCompat.checkSelfPermission(this, permission);
return (check == PackageManager.PERMISSION_GRANTED);
}

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.sendsms2.MainActivity"
android:orientation="vertical"
android:gravity="center">

<EditText
android:id="@+id/inputNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:hint="@string/number"/>

<EditText
android:id="@+id/inputMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/message"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="2000"
android:maxLines="5"/>

<Button
android:id="@+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"
android:onClick="onSend"/>
</LinearLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendsms2">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SendSMS2">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
</application>

</manifest>

Output :

You might also like