android - 检查 sdk 版本后缺少 PendingIntent 可变性标志给出警告

标签 android kotlin warnings android-pendingintent

嘿,我收到未决 Intent 的警告。所以我根据这个question和这个medium帖子围绕检查sdk进行检查。我收到警告信息
缺少 PendingIntent 可变性标志

val pendingIntent: PendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
            } else {
                PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
            }
enter image description here
如何删除此警告消息?
enter image description here

最佳答案

你的代码看起来不错,我相信这是 Lint 检查中的一个错误,因为它一直是
@CommonsWare 在评论中声明。这可以在 Android Studio 的下一个版本中修复

How can I remove this warning message?


如果您只想删除烦人的警告,则在构建清除警告的条件时有一个技巧:通过将条件转移到标志:
val pendingIntent: PendingIntent = PendingIntent.getActivity(
    this,
    0,
    intent,
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    else PendingIntent.FLAG_UPDATE_CURRENT
)
或者在更坏的情况下,你会通过 @SuppressLint("UnspecifiedImmutableFlag") 来压制它。 ,我不推荐。

关于android - 检查 sdk 版本后缺少 PendingIntent 可变性标志给出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70652465/

相关文章:

android - 横向模式下的全屏 VideoView

android - Ubuntu Android Studio Gradle 同步错误 : configuration with name 'default' not found

kotlin - 基于 Ktor 的网站的国际化

perl - Perl 中警告传播的最佳实践

.net - 如何将生成的方法标记为过时?

android - Tika 与安卓兼容吗?

java - Android数据库游标具体是如何实现的?

android - Kotlin : How to insert a value of a variable in a name of another variable

kotlin - 使用Kotlin契约在Iterable函数谓词中强制转换类型

php - 警告 : filesize function failing to operate