在android 11中使用intent.resolveActivity时调用此方法时,请考虑向清单中添加查询声明 [英] Consider adding a queries declaration to your manifest when calling this method when using intent.resolveActivity in android 11
问题描述
我有一个扩展功能,可以为我的活动打开意图:
I've an extension function for opening an intent for my activities:
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)
中给我一个警告:
My targetSdkVersion
is 30. It gives me a warning in intent.resolveActivity(it)
:
考虑在调用此方法时向清单中添加查询声明.
Consider adding a queries declaration to your manifest when calling this method.
那我该怎么办才能解决此警告?
So What should I do to solve this warning?
推荐答案
最简单的解决方案是摆脱 resolveActivity()
.替换:
The simplest solution is to get rid of resolveActivity()
. Replace:
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)
}
这将为您提供相同的结果,但性能会有所提高,并且可以消除警告.
This gives you the same result with a bit better performance, and it will get rid of the warning.
另一种选择是添加 QUERY_ALL_PACKAGES
权限.这可能会让您被Play商店禁止.
Another option would be to add the QUERY_ALL_PACKAGES
permission. This may get you banned from the Play Store.
否则,您将需要:
-
为您的应用可能进行的每个
openIntent()
调用构建列表
添加< queries>清单中的
元素,其中列出了您希望能够与 resolveActivity()
定期重复此过程,以防为 openIntent()
Repeat this process periodically, in case you add new scenarios for openIntent()
这篇关于在android 11中使用intent.resolveActivity时调用此方法时,请考虑向清单中添加查询声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!