我当前的 Android 项目允许用户在浏览器中打开 Web 链接或发送电子邮件到单击的电子邮件地址。
我的意图如下:-
fun sendEmail(recipient: String, subject: String, message: String) {
val uriText = "${EMAIL_MAIL_TO}${recipient}?subject=${subject}&body=${message}\n\n"
val emailIntent = Intent(Intent.ACTION_SENDTO, Uri.parse(uriText)).apply {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, message)
}
startActivity(Intent.createChooser(emailIntent, "Contact $recipient"))
}
和
fun openBrowser(url: String) {
val browserIntent = Intent(Intent.ACTION_VIEW).apply {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
addCategory(Intent.CATEGORY_BROWSABLE)
data = Uri.parse(url)
}
startActivity(Intent.createChooser(browserIntent, "Open Web Url"))
}
上述代码适用于发送电子邮件和在所需网页上打开设备网络浏览器。
当我将
createChooser
替换为 时
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
在这两种情况下,
resolveActivity
函数都返回 null。
我做错了什么?
https://developer.android.com/training/package-visibility/use-cases#check-browser-available
<- Place inside the "
<queries>
”元素。->
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
这些清单条目解决了我的问题
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
</intent>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="*" />
</intent>
</queries>
奇怪的是,在我所有的像素测试设备上,我必须按回两次才能返回我的应用程序,而在三星设备上,我只需按回一次。