gpt4 book ai didi

android - 在 android 11 中使用 intent.resolveActivity 时调用此方法时,请考虑在 list 中添加查询声明

转载 作者:行者123 更新时间:2023-12-03 13:46:45 36 4
gpt4 key购买 nike

我有一个扩展功能,用于为我的 Activity 打开一个 Intent :

fun Activity.openIntent(action: String?, type: String?, uri: Uri?) {
Intent()
.apply {
action?.let { this.action = it }
uri?.let { this.data = it }
type?.let { this.type = it }
}
.also { intent ->
packageManager?.let {
if (intent.resolveActivity(it) != null)
startActivity(intent)
else
showToast(R.string.application_not_found)
}
}
}
我的 targetSdkVersion 30 .它在 intent.resolveActivity(it) 中给了我一个警告:

Consider adding a queries declaration to your manifest when calling this method.


那么我应该怎么做才能解决这个警告呢?

最佳答案

最简单的解决方案是去掉 resolveActivity() .代替:

            packageManager?.let {
if (intent.resolveActivity(it) != null)
startActivity(intent)
else
showToast(R.string.application_not_found)
}
和:
            try {
startActivity(intent)
} catch (ex: ActivityNotFoundException) {
showToast(R.string.application_not_found)
}
这为您提供了相同的结果和更好的性能,并且它会消除警告。
另一种选择是添加 QUERY_ALL_PACKAGES允许。这可能会让您被禁止进入 Play 商店。
否则,您将需要:
  • 建立所有可能的列表openIntent()您的应用可能会调用的电话
  • Add a <queries> element to your manifest列出所有可能的Intent您希望能够与 resolveActivity() 一起使用的结构
  • 定期重复此过程,以防您为 openIntent() 添加新方案
  • 关于android - 在 android 11 中使用 intent.resolveActivity 时调用此方法时,请考虑在 list 中添加查询声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64945660/

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