嘿,我收到未决 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)
}
如何删除此警告消息?
最佳答案
你的代码看起来不错,我相信这是 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/