Android 12 Kotlin-强烈考虑使用Flag_immutable,仅在某些功能取决于悬浮词是可变的情况下才能使用flag_mutable

发布于 2025-01-26 04:25:01 字数 1030 浏览 2 评论 0原文

这是悬念的代码:

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.

我仍然需要更新活动,因此如何添加此flag_immutableflag_mutable到底是,仍然能够更新活动?基于我尝试的答案:

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

但是这给出了语法错误!

那么代码应该如何看?

This is the code for pendingIntent:

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

I'm getting this error when using it:

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.

I still need the activity to be updated so how can I add this FLAG_IMMUTABLE or FLAG_MUTABLE what ever the hell this is and still be able to update the activity? Based on this answer I tried:

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

but this gives syntax error!

So how should the code look?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

基于此答案

该问题和答案中目前显示的代码在Java中。您正在Kotlin写作。

在kotlin中,使用 或 >::

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

Based on this answer

The code presently shown in that question and answer are in Java. You are writing in Kotlin.

In Kotlin, use the or bitwise operator:

val pi =
    PendingIntent.getActivity(
        applicationContext,
        0,
        ii,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文