Android 12 Kotlin - 强烈考虑使用 FLAG_IMMUTABLE,只有在某些功能依赖于可变的 PendingIntent 时才使用 FLAG_MUTABLE

Android 12 Kotlin - Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable

这是 pendingIntent 的代码:

val pi =
    PendingIntent.getActivity(
        applicationContext,
        0,
        ii,
        PendingIntent.FLAG_UPDATE_CURRENT
    )

我在使用时遇到这个错误:

java.lang.IllegalArgumentException: de.xxx.xxx: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

我仍然需要更新 activity 那么我怎样才能添加这个 FLAG_IMMUTABLEFLAG_MUTABLE 这到底是怎么回事并且仍然能够更新 activity?基于 我试过的答案:

val pi =
    PendingIntent.getActivity(
        applicationContext,
        0,
        ii,
        PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
    )

但这给出了语法错误!

那么代码应该怎么看?

Based on this answer

该问题和答案中当前显示的代码在 Java 中。您正在使用 Kotlin 编写。

在 Kotlin 中,使用 the or bitwise operator:

val pi =
    PendingIntent.getActivity(
        applicationContext,
        0,
        ii,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )