在 android 11 中使用 intent.resolveActivity 时调用此方法时,请考虑向您的清单添加查询声明
Consider adding a queries declaration to your manifest when calling this method when using intent.resolveActivity in android 11
我有一个扩展功能可以打开我的活动意图:
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 列出了您希望能够与 resolveActivity()
一起使用的所有可能的 Intent
结构
定期重复此过程,以防您为 openIntent()
添加新方案
因此,从 Android 11 开始(即,如果您的应用程序面向 Android 11),并非所有应用程序都对您的应用程序可见。一些应用是 visible by default but in order to access other applications through your application, you will have to declare queries
in your manifest else your application will not be able to access them. You can read about that here.
因此,如果您的应用程序面向 Android 11 并且要访问默认情况下可能不可见的应用程序,您需要在清单文件中为它们添加 queries
。
对于您的情况,此警告不适用,因为我认为您正在使用隐式意图打开其他应用程序。使用隐式意图,无论应用程序可见性如何,都可以访问其他应用程序。如果您的应用程序目标 Android 10 或更低,您可以取消警告,因为默认情况下所有应用程序都是可见的。
要抑制 lint 警告,您可以:
- 添加抑制注释,如下所示:
@SuppressLint("QueryPermissionsNeeded")
fun Activity.openIntent(action: String?, type: String?, uri: Uri?): Activity {
- 将以下内容添加到您的应用程序模块构建 gradle 文件中的 android 块:
lintOptions {
ignore "QueryPermissionsNeeded"
}
替换
if (intent.resolveActivity(it) != null)
和
if (it.resolveActivity(intent, 0) != null)
警告将消失。
我有一个扩展功能可以打开我的活动意图:
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 列出了您希望能够与resolveActivity()
Intent
结构定期重复此过程,以防您为
添加新方案openIntent()
因此,从 Android 11 开始(即,如果您的应用程序面向 Android 11),并非所有应用程序都对您的应用程序可见。一些应用是 visible by default but in order to access other applications through your application, you will have to declare queries
in your manifest else your application will not be able to access them. You can read about that here.
因此,如果您的应用程序面向 Android 11 并且要访问默认情况下可能不可见的应用程序,您需要在清单文件中为它们添加 queries
。
对于您的情况,此警告不适用,因为我认为您正在使用隐式意图打开其他应用程序。使用隐式意图,无论应用程序可见性如何,都可以访问其他应用程序。如果您的应用程序目标 Android 10 或更低,您可以取消警告,因为默认情况下所有应用程序都是可见的。
要抑制 lint 警告,您可以:
- 添加抑制注释,如下所示:
@SuppressLint("QueryPermissionsNeeded")
fun Activity.openIntent(action: String?, type: String?, uri: Uri?): Activity {
- 将以下内容添加到您的应用程序模块构建 gradle 文件中的 android 块:
lintOptions {
ignore "QueryPermissionsNeeded"
}
替换
if (intent.resolveActivity(it) != null)
和
if (it.resolveActivity(intent, 0) != null)
警告将消失。