Kotlin PendingIntent with FLAG_IMMUTABLE 不适用于 exoplayer



当我在我的音乐应用程序中播放歌曲时,我正在使用一个挂起的意图来制作通知栏。它曾经工作得很好,但我必须将我的targetSdk更新为31,我遇到一个错误,告诉我我需要使用一些标志与我的pendingIntent:

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.

问题是,当我声明我的挂起意图时,我使用这个标志:

val pendingIntent = PendingIntent.getActivity(
applicationContext,
0,
packageManager?.getLaunchIntentForPackage(packageName),
PendingIntent.FLAG_IMMUTABLE
)

所以我不明白为什么它说我需要使用的标志,当我已经使用它?我错过什么了吗?

编辑

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.
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4953)
at android.app.ActivityThread.access$1900(ActivityThread.java:310)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2300)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8663)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
Caused by: java.lang.IllegalArgumentException: info.cairn.pro: 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.
at android.app.PendingIntent.checkFlags(PendingIntent.java:382)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:673)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:660)
at com.google.android.exoplayer2.ui.PlayerNotificationManager.createBroadcastIntent(PlayerNotificationManager.java:1601)
at com.google.android.exoplayer2.ui.PlayerNotificationManager.createPlaybackActions(PlayerNotificationManager.java:1557)
at com.google.android.exoplayer2.ui.PlayerNotificationManager.<init>(PlayerNotificationManager.java:862)
at com.google.android.exoplayer2.ui.PlayerNotificationManager.<init>(PlayerNotificationManager.java:793)
at com.google.android.exoplayer2.ui.PlayerNotificationManager.<init>(PlayerNotificationManager.java:757)
at com.google.android.exoplayer2.ui.PlayerNotificationManager.createWithNotificationChannel(PlayerNotificationManager.java:729)
at com.viapresse.presskit.exoplayer.MusicNotificationManager.<init>(MusicNotificationManager.kt:29)
at com.viapresse.presskit.exoplayer.MusicService.onCreate(MusicService.kt:114)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4940)

根据@ianhanniballake的建议,崩溃发生在exoplayer依赖项中,而不是在我的代码中…

所以我去https://github.com/google/ExoPlayer/blob/release-v2/RELEASENOTES.md检查所有exoplayer更新,我发现他们在哪里实现了PendingIntent。FLAG_IMMUTABLE是一个有趣的东西,它在2.14.2中只比我使用的迭代高一次。

所以我更新了对exoplayer的依赖,一切都工作了。

最新更新