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

标签 android android-intent push-notification

这是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 ?基于 this我试过的答案:
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
    )

关于Android 12 Kotlin - 强烈考虑使用 FLAG_IMMUTABLE,仅当某些功能依赖于 PendingIntent 可变时才使用 FLAG_MUTABLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72120158/

相关文章:

Android:如何在 fragment 中创建带有 float RecyclerView 的叠加层

android - 我无法将我的位图保存到 Android 中的图库中?

android - 如何将图像添加到 ListView (Rss Feed)

javascript - Web 推送通知的权限更改是否有回调?

swift - 在后台 Swift 中跟踪推送通知

android - Node.js GCM 推送通知允许的 ID 数量?

java - 对话框关闭在适配器内部不起作用

image - android中的谷歌图像搜索问题

android - 如何将从相机 Intent 拍摄的图像设置为 ImageView?

java - 如何修复 "startActivity() in ContextCompat cannot be applied to?"