为什么 Intent.EXTRA_INITIAL_INTENTS 在 android 10 中不起作用

Bin*_*Bin 6 android

我正在尝试分享一些文字,在 Android 10 中总是在我的手机中显示最多 3 个应用程序有 WhatsApp 但这里没有显示

此代码在 10 台以下设备中正常工作,但找不到在 android 10 中被过滤掉的原因。

fun onShareClick() {
        val intentShareList = ArrayList<Intent>()
        val shareIntent = Intent()
        shareIntent.action = Intent.ACTION_SEND
        shareIntent.type = "text/plain"
        val resolveInfoList = packageManager.queryIntentActivities(shareIntent, 0)
        for (resInfo in resolveInfoList) {
            val packageName = resInfo.activityInfo.packageName
            val name = resInfo.activityInfo.name
            if (packageName.contains("com.facebook") ||
                packageName.contains("com.twitter.android") ||
                packageName.contains("com.google.android.gm") ||
                packageName.contains("com.android.mms") ||
                packageName.contains("com.whatsapp")
            ) {
                val intent = Intent()
                intent.component = ComponentName(packageName, name)
                intent.action = Intent.ACTION_SEND
                intent.type = "text/plain"
                intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject")
                intent.putExtra(Intent.EXTRA_TEXT, "Your Content")
                intentShareList.add(intent)
            }
        }
        if (intentShareList.isEmpty()) {
            Toast.makeText(this@MainActivity, "No apps to share !", Toast.LENGTH_SHORT).show()
        } else {
            val chooserIntent = Intent.createChooser(intentShareList.removeAt(0), "Share via")
            chooserIntent.putExtra(
                Intent.EXTRA_INITIAL_INTENTS,
                intentShareList.toTypedArray()
            )
            startActivity(chooserIntent)
        }
    }
Run Code Online (Sandbox Code Playgroud)

intentShareList包含 WhatsApp 信息但不显示

Mar*_*lho 2

我认为您应该使用初始意图创建选择器,并将您的intentShareList添加为额外的,如下所示:

val chooserIntent = Intent.createChooser(shareIntent, "Share via")
        chooserIntent.putExtra(
            Intent.EXTRA_INITIAL_INTENTS,
            intentShareList.toTypedArray()
        )
        startActivity(chooserIntent)
Run Code Online (Sandbox Code Playgroud)

我想它会带来你想要的。如果有帮助请告诉我:)

编辑:即使您在 shareIntent 上添加了初始 EXTRA_INTENT 和类型,Android Q 也会忽略您在 EXTRA_INITIAL_INTENTS 上添加的内容,因此它不会有帮助

我正在阅读有关该问题的文章,我认为它不会得到解决,请看一下= https://issuetracker.google.com/issues/134367295

另外,文档上有一些信息说不建议使用 EXTRA_INITIAL_INTENTS 来个性化数据,它是为了帮助共享链接,并且可以减少将出现的应用程序,看看 = https://developer.android.com/培训/分享/发送

好吧,我对此感到非常难过,我也不知道该怎么办,也许是一个独特的文本......:(