为什么当Intent.createChooser工作正常时Android的resolveActivity返回null

问题描述 投票:0回答:2

我当前的 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。

我做错了什么?

android android-intent
2个回答
1
投票

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>

0
投票

这些清单条目解决了我的问题

<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>

奇怪的是,在我所有的像素测试设备上,我必须按回两次才能返回我的应用程序,而在三星设备上,我只需按回一次。

© www.soinside.com 2019 - 2024. All rights reserved.