我在 Android 中遇到了电子邮件意图问题。
收件人字段未正确填充。
我的代码是这样的:
扩展.kt
// Returns a Mail Intent
fun requireMailIntent(subject: String, body: String) = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:")
putExtra(Intent.EXTRA_EMAIL, arrayOf("email@gmail.com"))
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT,body)
}
/**
* Checks whether the intent has an app that can
* handle it.
* Should be called before starting an intent
**/
fun Intent.hasSuccessor(context: Context) = resolveActivity(context.packageManager) != null
片段.kt
// Submit button
binding.ButtonSubmit.setOnClickListener {
val emailConstruct = constructEmail()
val intent = requireMailIntent(emailConstruct.first, emailConstruct.second)
if(intent.hasSuccessor(requireContext())){
Log.v("INTENT_TEST", "Launching Intent")
startActivity(intent)
}else{
Log.v("INTENT_TEST", "No app found")
}
}
private fun constructEmail(): Pair<String,String>{
val subject = "MES :: Bug Report :: ${viewModel.bugIdentified}"
val message = "Below are the steps \n ${viewModel.bugSteps}"
return Pair(subject, message)
}
清单.xml
<!-- For basic package querying, ie browsers, email... -->
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
摇篮
// SDK Versions
sdk_compiled_version = 30
sdk_minimum_version = 24
// Build tools
build_tools = "29.0.3"
单击按钮后,Android 会向我显示应用程序选择器,但是当我单击 Gmail 时,除了收件人字段之外的所有内容都会填充。
有人可以帮忙吗?