0% found this document useful (0 votes)
11 views1 page

Lab Preppp

The document provides code snippets for an Android application that demonstrates the use of explicit intents to open different apps, including the phone, mail, and SMS applications. It includes an XML layout file for the main activity with a button to open the phone app and Kotlin code for handling button clicks to initiate calls, send emails, and send SMS messages. The examples illustrate how to create intents with specific actions and data URIs for each functionality.
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)
11 views1 page

Lab Preppp

The document provides code snippets for an Android application that demonstrates the use of explicit intents to open different apps, including the phone, mail, and SMS applications. It includes an XML layout file for the main activity with a button to open the phone app and Kotlin code for handling button clicks to initiate calls, send emails, and send SMS messages. The examples illustrate how to create intents with specific actions and data URIs for each functionality.
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/ 1

Must Know

Using explicit intent (can be used for EX 08 sending mail or 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/openPhoneAppButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Phone App"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt (for opening phone)


import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


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

val MyButton: Button = findViewById(R.id.openPhoneAppButton)

MyButton.setOnClickListener{
val intent = Intent(Intent.ACTION_DIAL)
intent.data = Uri.parse("tel: 9080819643")
startActivity(intent)
}

}
}

(for opening Mail app)


btn.setOnClickListener{
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto: sam@karunya.edu.in")
startActivity(intent)
}

(for opening SMS app)

btn.setOnClickListener{
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("sms: 9080819643")
intent.putExtra("sms_body", "It is critical")
startActivity(intent)
}

You might also like