gpt4 book ai didi

android - intent.resolveActivity 在 API 30 中返回 null

转载 作者:行者123 更新时间:2023-12-03 07:30:05 25 4
gpt4 key购买 nike

intent.resolveActivity != null but launching the intent throws an ActivityNotFound exception我写了使用深度链接打开浏览器或应用程序:

private fun openUrl(url: String) {
val intent = Intent().apply {
action = Intent.ACTION_VIEW
data = Uri.parse(url)
// setDataAndType(Uri.parse(url), "text/html")
// component = ComponentName("com.android.browser", "com.android.browser.BrowserActivity")
// flags = Intent.FLAG_ACTIVITY_CLEAR_TOP + Intent.FLAG_GRANT_READ_URI_PERMISSION
}
val activityInfo = intent.resolveActivityInfo(packageManager, intent.flags)
if (activityInfo?.exported == true) {
startActivity(intent)
} else {
Toast.makeText(
this,
"No application can handle the link",
Toast.LENGTH_SHORT
).show()
}
}
它不起作用。在 API 30 模拟器中找不到浏览器,而常见的 solution作品:
private fun openUrl(url: String) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
Toast.makeText(
this,
"No application can handle the link",
Toast.LENGTH_SHORT
).show()
}
}
第一种方法不行,因为 intent.resolveActivityInfointent.resolveActivity返回 null .但对于 PDF 查看器,它 works .
我们应该解雇 intent.resolveActivity ?

最佳答案

这似乎是由于 the new restrictions on "package visibility" introduced in Android 11 .
基本上,从 API 级别 30 开始,如果您的目标是该版本或更高版本,您的应用程序无法看到或直接与大多数外部包交互,除非通过一揽子 QUERY_ALL_PACKAGES 明确请求许可。许可,或包含适当的 <queries> list 中的元素。
实际上,您的第一个 fragment 在该权限或适当的 <queries> 下按预期工作。 list 中的元素;例如:

<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
</queries>

当前可用的信息并不是非常具体,但确实指出:

The PackageManager methods that return results about other apps, such as queryIntentActivities(), are filtered based on the calling app's <queries> declaration


尽管您的示例使用的是 Intent方法 – 即 resolveActivityInfo() – 这实际上是在调用 PackageManager内部的“查询”方法。受此更改影响的所有方法和功能的详尽列表可能不可行,但可以肯定的是,如果 PackageManager涉及到,你最好用新的限制检查它的行为。

关于android - intent.resolveActivity 在 API 30 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62535856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com