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

Viv*_*odi 8 android warnings kotlin android-pendingintent

嘿,我收到了未决意图的警告。所以我根据这个问题和这个中等帖子包围了检查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)
            }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如何删除此警告消息?

在此输入图像描述

Zai*_*ain 3

你的代码看起来没问题,我相信这是 Lint 检查中的一个错误,正如
@CommonsWare 在评论中所述。这可能会在 Android Studio 的下一版本中修复

如何删除此警告消息?

如果您只是想删除烦人的警告,则可以通过将条件转移到标志来构建清除警告的条件:

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
)
Run Code Online (Sandbox Code Playgroud)

或者在最坏的情况下,你会通过 来抑制它@SuppressLint("UnspecifiedImmutableFlag"),我不建议这样做。