我在android jetpack compose中有一個簡單的按鈕,當我點擊按鈕時,我想打開gmail并將郵件發送到“[email protected]”,這可能嗎?
@Composable
fun SimpleButton() {
Button(onClick = {
//your onclick code here
}) {
Text(text = "Simple Button")
}
}
uj5u.com熱心網友回復:
您必須創建一個 Intent,然后使用它啟動一個 Activity,類似于您通常必須執行的操作。
Compose 中的唯一區別是您獲得了Contextwith LocalContext.current。
@Composable
fun SimpleButton() {
val context = LocalContext.current
Column {
Button(onClick = {
context.sendMail(to = "[email protected]", subject = "Some subject")
}) {
Text(text = "Send mail")
}
Button(onClick = {
context.dial(phone = "12345678")
}) {
Text(text = "Dial number")
}
}
}
fun Context.sendMail(to: String, subject: String) {
try {
val intent = Intent(Intent.ACTION_SEND)
intent.type = "vnd.android.cursor.item/email" // or "message/rfc822"
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(to))
intent.putExtra(Intent.EXTRA_SUBJECT, subject)
startActivity(intent)
} catch (e: ActivityNotFoundException) {
// TODO: Handle case where no email app is available
} catch (t: Throwable) {
// TODO: Handle potential other type of exceptions
}
}
fun Context.dial(phone: String) {
try {
val intent = Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null))
startActivity(intent)
} catch (t: Throwable) {
// TODO: Handle potential exceptions
}
}
有關更多可能性,請參見此處的答案,但請記住有些已過時。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/494861.html
上一篇:取決于AndroidAPI級別的不同AndroidManifest內容
下一篇:android.content.res.Resources$NotFoundException:可繪制的compat_splash_screen_no_icon_background資源ID#0x7f
