Useful intents functionality to send mail, Call to the number and Show Direction on Map.
As application developer (android) you frequently need to implement common functionality like call, mail or show direction on the map.
//For open dial pad from android application
fun launchCall(mActivity: Activity, phoneNumber: String) {
val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phoneNumber"))
mActivity.startActivity(intent)
}
I used ACTION_DIAL instead of ACTION_CALl becuase action_call require permission define in menifest.xml and action_dial don’t require any. So later better to user.
fun launchMail(mActivity: Activity, emailAddress: String) {
val intent = Intent(Intent.ACTION_SENDTO)
intent.type = "text/html"
intent.putExtra(Intent.EXTRA_EMAIL, "emailaddress@gmail.com")
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject")
intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.")
mActivity.startActivity(Intent.createChooser(intent, "Send Email"))
}
Above code used to open Email client from application, used ACTION_SENDTO because its only show email client not other application i.e twitter, facebook etc.. so its better to user ACTION_SENDTO instead of ACTION_SEND.
fun launchMap(mActivity: Activity, lat: String, lng: String) {
val intent = Intent(
Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=$lat,$lng (Mas Insurance Group)")
)
mActivity.startActivity(intent)
}
To launch map with specif location call Intent.ACTION_VIEW and set Geo location.
One can simply create IntentHelper class and put all related method there, which will easy to use.