待处理意图可变性问题

huangapple go评论43阅读模式
英文:

pendingIntent mutability issues

问题

我已经知道,如果SDK版本超过S,则在使用PendingIntent时必须设置可变性,所以我编写了如下代码,

val pendingIntent = PendingIntent.getActivity(this, 0, intent, 
PendingIntent.FLAG_UPDATE or PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE)

这是我唯一使用PendingIntent类的地方,有时Firebase分析消息会发送给我以下错误消息:

致命异常:java.lang.IllegalArgumentException
MY_APP_PACKAGE: 针对S+(版本31及以上),在创建PendingIntent时必须指定FLAG_IMMUTABLE或FLAG_MUTABLE中的一个。
强烈建议使用FLAG_IMMUTABLE,仅在某些功能依赖于PendingIntent可变性时使用FLAG_MUTABLE,例如,如果需要与内联回复或气泡一起使用。

我搜索了很多关于这个消息的问题,但没有人像我一样遇到这个问题。

在我的代码中收到这个消息的原因是什么?

或者是否有其他问题可能引发此错误消息?

英文:

i already know if sdk versions over S, must be set mutability when using PendingIntent

so i coded like this,

val pendingIntent = PendingIntent.getActivity(this, 0, intent, 
PendingIntent.FLAG_UPDATE or PendingIntent.FLAG_CANCEL_CURENT or PendingIntent.FLAG_IMMUTABLE)

that is only place where i using PendingIntent class and somtime, firebase analytics messages sending to me

Fatal Exception: java.lang.IllegalArgumentException
MY_APP_PACKAGE: 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 searched many questions for this message but there is no one got stucked like me

is there any reason to get this message in my code?

or any other problem can cause this error message?

答案1

得分: 1

当针对S+(版本31或更高版本)时,在创建PendingIntent时,必须指定FLAG_IMMUTABLE或FLAG_MUTABLE中的一个,否则会报错。Google Docs强烈建议使用FLAG_IMMUTABLE,并告诉您仅在某些功能依赖可变PendingIntent时使用FLAG_MUTABLE。

[如何修复错误]

如果将以下库添加到应用级别的build.gradle文件中并运行应用程序,错误将得以解决。
如果您的应用程序使用的是AdMob 20.4.0或更早版本,则必须添加以下任务管理器依赖项。

implementation 'androidx.work:work-runtime:2.7.1';

但是,如果源代码中使用了PendingIntent的代码,必须指定一个标志值。

我会提供相关的代码。这对我有用。

PendingIntent sPendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    sPendingIntent = PendingIntent.getActivity(context,
            NOTIFICATION_ID, sIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}else {
    sPendingIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, sIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
英文:

When targeting S+ (version 31 or later), it is an error report that either FLAG_IMMUTABLE or FLAG_MUTABLE must be specified when creating a PendingIntent. Google Docs strongly consider using FLAG_IMMUTABLE and tell you to use FLAG_MUTABLE only if some features rely on changeable PendingIntent.

[How to fix the error]

If you add the following library to the app-level build.gradle file and run app, the error will be solved.
If your app uses AdMob 20.4.0 or earlier, you must add the following Task Manager dependencies.

implementation 'androidx.work:work-runtime:2.7.1'

However, if there is a code that is using PendingIntent on the source code, it is necessary to specify a flag value.

I'll give you my relevant code. This worked for me.

PendingIntent sPpendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    sPpendingIntent = PendingIntent.getActivity(context,
            NOTIFICATION_ID, sIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}else {
    sPpendingIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, sIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}

huangapple
  • 本文由 发表于 2023年7月13日 16:17:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定