0% found this document useful (0 votes)
98 views

Mobile Application Development Lab Project

This document provides code samples for creating an SMS application in Android Studio that allows sending and receiving SMS messages. It includes XML layout code, manifest code, and Java code to build a basic SMS app interface with a button to send messages and a text view to display received messages. Permission configurations are also demonstrated to allow sending and receiving SMS.

Uploaded by

Harshitha Gowda
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)
98 views

Mobile Application Development Lab Project

This document provides code samples for creating an SMS application in Android Studio that allows sending and receiving SMS messages. It includes XML layout code, manifest code, and Java code to build a basic SMS app interface with a button to send messages and a text view to display received messages. Permission configurations are also demonstrated to allow sending and receiving SMS.

Uploaded by

Harshitha Gowda
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/ 18

MOBILE APPLICATION DEVELOPMENT

LAB PROJECT

Sending SMS, Receiving SMS


In this article, We will show you how to create an SMS
Android App using Android Studio. SMS stands for Short
Message Service and is also commonly referred to as a "text
message". With an SMS app, you can send a message of up
to 160 characters to another device. Longer messages will
automatically be split into several parts. Most cell phones
support this type of text messaging.

 Sending SMS
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/andr
oid"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.messaging.MainActivity">
<Button
android:text="Send SMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSendSMS"
app:layout_constraintLeft_toLeftOf="@+id/activity_main"
app:layout_constraintTop_toTopOf="@+id/activity_main"
android:layout_marginTop="16dp"
app:layout_constraintRight_toRightOf="@+id/activity_mai
n"
app:layout_constraintBottom_toBottomOf="@+id/activity_
main"
android:layout_marginBottom="16dp"
android:onClick="onClick" />
</androidx.constraintlayout.widget.ConstraintLayout>

Manifest Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/andr
oid"
package="com.example.messaging">
<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.Messaging">
<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>

JAVA Code:
package com.example.messaging;
import android.Manifest;
import android.content.pm.PackageManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Toast;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
final private int REQUEST_SEND_SMS = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
REQUEST_SEND_SMS);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[]
grantResults) {
switch (requestCode) {
case REQUEST_SEND_SMS:
if (grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Permission Granted",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Permission Denied",
Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode,
permissions, grantResults);
}
}
public void onClick(View v) {
//---the "phone number" of your emulator should be 5554--
-
sendSMS("5554", "Hello\n How are you?");
}
//---sends an SMS message---
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null,
null);
}
}

 Receiving SMS
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/andr
oid"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.messaging.MainActivity">
<Button
android:text="Send SMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSendSMS"
app:layout_constraintLeft_toLeftOf="@+id/activity_main"
app:layout_constraintTop_toTopOf="@+id/activity_main"
android:layout_marginTop="16dp"
app:layout_constraintRight_toRightOf="@+id/activity_mai
n"
app:layout_constraintBottom_toBottomOf="@+id/activity_
main"
android:layout_marginBottom="16dp"
android:onClick="onClick" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
app:layout_constraintLeft_toLeftOf="@+id/btnSendSMS"
app:layout_constraintTop_toBottomOf="@+id/btnSendSM
S"
android:layout_marginTop="8dp"
app:layout_constraintRight_toRightOf="@+id/btnSendSMS
"
app:layout_constraintBottom_toBottomOf="@+id/activity_
main"
android:layout_marginBottom="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

MANIFEST Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/andr
oid"
package="com.example.messaging">
<uses-permission
android:name="android.permission.SEND_SMS"/>
<uses-permission
android:name="android.permission.RECEIVE_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.Messaging">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SMSReceiver"
android:exported="true"

android:permission="android.permission.BROADCAST_SMS
">
<intent-filter android:priority="9000">
<action
android:name="android.provider.Telephony.SMS_RECEIVE
D" />
</intent-filter>
</receiver>
</application>
</manifest>

JAVA Code:
package com.example.messaging;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.Manifest;
import android.content.pm.PackageManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.content.BroadcastReceiver;
import android.widget.TextView;
import android.widget.Toast;
import androidx.core.content.ContextCompat;
import static android.Manifest.permission_group.SMS;
public class MainActivity extends AppCompatActivity {
final private int REQUEST_SEND_SMS = 123;
final private int REQUEST_REC_SMS = 321;
BroadcastReceiver smsSentReceiver;
IntentFilter intentFilter;
private BroadcastReceiver intentReceiver = new
BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//---display the SMS received in the TextView---
TextView SMSes = (TextView) findViewById(R.id.textView);
SMSes.setText(intent.getExtras().getString("sms"));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
REQUEST_SEND_SMS);
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECEIVE_SMS)
!= PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECEIVE_SMS},
REQUEST_REC_SMS);
}
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
}
@Override
public void onResume() {
super.onResume();
//---register the receiver---
registerReceiver(intentReceiver, intentFilter);
}
@Override
public void onPause() {
super.onPause();
//---unregister the receiver---
unregisterReceiver(intentReceiver);
}
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[]
grantResults) {
switch (requestCode) {
case REQUEST_SEND_SMS:
if (grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"SEND Permission Granted",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
"SEND Permission Denied", Toast.LENGTH_SHORT).show();
}
break;
case REQUEST_REC_SMS:
if (grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"RECEIVE Permission Granted",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
"RECEIVE Permission Denied",
Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode,
permissions, grantResults);
}
}
public void onClick(View v) {
sendSMS("5554", "Hello my friends!");
}
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null,
null);
}
}
SMSReceiver Code:
package com.example.messaging;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "SMS from ";
if (bundle != nul
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
if (i==0) {
//---get the sender address/phone number---
str += msgs[i].getOriginatingAddress();
str += ": ";
}
//---get the message body---
str += msgs[i].getMessageBody().toString();
}
//---display the new SMS message---
Toast.makeText(context, str,
Toast.LENGTH_SHORT).show();
Log.d("SMSReceiver", str);
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
}
}
}

You might also like