android kotlin 呼叫(發起)email app

好多種版本, 終於找到可以用的

  • 第一種寫法(比較適合寫慣java的老師傅)
val email_intent =  Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
    email_intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    email_intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("your@email.com"))
    email_intent.putExtra(Intent.EXTRA_SUBJECT, "問題回報");
    email_intent.putExtra(Intent.EXTRA_TEXT,"");
    try {
      startActivity(Intent.createChooser(email_intent, "請選擇郵件軟體"))
    }catch (e:ActivityNotFoundException){
      Toast.makeText(myactivity, "請確認並設定好郵件收發軟體", Toast.LENGTH_LONG).show()
    }
  • 第二種寫法
val email_intent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:")).apply {
    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    putExtra(Intent.EXTRA_EMAIL, arrayOf("your@email.com"))
    putExtra(Intent.EXTRA_SUBJECT, "問題回報")
    putExtra(Intent.EXTRA_TEXT, "")
  }
  try {
    startActivity(Intent.createChooser(email_intent, "請選擇郵件軟體"))
  } catch ( ex: ActivityNotFoundException) {
    Toast.makeText(myactivity, "請確認並設定好郵件收發軟體", Toast.LENGTH_LONG).show()
  }

發表迴響