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

Practical 7

Uploaded by

Meet Savaliya
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)
9 views4 pages

Practical 7

Uploaded by

Meet Savaliya
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/ 4

Marwadi University

Faculty of Technology
Department of Information and Communication Technology
Subject: Mobile and Pervasive Aim: Develop an application that auto sends received SMS.
Computing [01CT0716]

Practical No: 7 Date: 12-10-2024 Enrollment No: - 92210133007

CODE:-

MainActivity.kt:-

class MainActivity : AppCompatActivity() {

private val SMS_PERMISSION_CODE = 101


private val TEXT_LIMIT = 160 // Set your desired text limit

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val phoneNumberEditText = findViewById<EditText>(R.id.phoneNumberEditText)


val messageEditText = findViewById<EditText>(R.id.messageEditText)
val sendButton = findViewById<Button>(R.id.sendButton)

sendButton.setOnClickListener {
val phoneNumber = phoneNumberEditText.text.toString().trim()
var message = messageEditText.text.toString().trim()

if (phoneNumber.isEmpty() || message.isEmpty()) {
Toast.makeText(this, "Please enter both phone number and message", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}

// Limit message to TEXT_LIMIT


if (message.length > TEXT_LIMIT) {
message = message.substring(0, TEXT_LIMIT)
Toast.makeText(this, "Message truncated to $TEXT_LIMIT characters", Toast.LENGTH_SHORT).show()
}

// Check SMS permission


if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.SEND_SMS),

1|Page
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Mobile and Pervasive Aim: Develop an application that auto sends received SMS.
Computing [01CT0716]

Practical No: 7 Date: 12-10-2024 Enrollment No: - 92210133007

SMS_PERMISSION_CODE)
} else {
sendSMS(phoneNumber, message)
}
}
}

private fun sendSMS(phoneNumber: String, message: String) {


try {
val smsManager = SmsManager.getDefault()
smsManager.sendTextMessage(phoneNumber, null, message, null, null)
Toast.makeText(this, "SMS sent successfully", Toast.LENGTH_SHORT).show()
} catch (e: Exception) {
Toast.makeText(this, "SMS failed to send", Toast.LENGTH_SHORT).show()
}
}

// Handle permission result


override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults:
IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == SMS_PERMISSION_CODE) {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
}
}
}
}

2|Page
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Mobile and Pervasive Aim: Develop an application that auto sends received SMS.
Computing [01CT0716]

Practical No: 7 Date: 12-10-2024 Enrollment No: - 92210133007

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SMSApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


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

</manifest>

3|Page
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Mobile and Pervasive Aim: Develop an application that auto sends received SMS.
Computing [01CT0716]

Practical No: 7 Date: 12-10-2024 Enrollment No: - 92210133007

Output:-

4|Page

You might also like