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)
}