MediaSessionCompat:Targeting S+ (version 31 and above) 要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一 [英] MediaSessionCompat:Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent
问题描述
我正在尝试将我的应用程序更新到 Android SDK 31,但我遇到了 MediaSessionCompat 问题.
我有一个 MediaService,它扩展了 MediaBrowserServiceCompat(),并在该服务的 onCreate 方法中初始化了 MediaSessionCompat.
override fun onCreate() {super.onCreate()mediaSession = MediaSessionCompat(this, TAG).apply {setCallback(mediaSessionCallback)isActive = 真}...
但是我遇到了以下错误
java.lang.RuntimeException: Unable to create service com.radio.core.service.MediaService: java.lang.IllegalArgumentException: com.xxx.xxx: Targeting S+ (version 31 and above) requires the one of the创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE .强烈考虑使用 FLAG_IMMUTABLE,仅当某些功能依赖于 PendingIntent 可变时才使用 FLAG_MUTABLE,例如如果需要与内联回复或气泡一起使用.在 android.app.ActivityThread.handleCreateService(ActivityThread.java:4498)在 android.app.ActivityThread.access$1500(ActivityThread.java:250)在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2064)在 android.os.Handler.dispatchMessage(Handler.java:106)在 android.os.Looper.loopOnce(Looper.java:201)在 android.os.Looper.loop(Looper.java:288)在 android.app.ActivityThread.main(ActivityThread.java:7829)在 java.lang.reflect.Method.invoke(Native Method)在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:982)引起: java.lang.IllegalArgumentException: com.xxx.xxx: Targeting S+ (version 31 and above) 要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一.强烈考虑使用 FLAG_IMMUTABLE,仅当某些功能依赖于 PendingIntent 可变时才使用 FLAG_MUTABLE,例如如果需要与内联回复或气泡一起使用.在 android.app.PendingIntent.checkFlags(PendingIntent.java:375)在 android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)在 android.app.PendingIntent.getBroadcast(PendingIntent.java:632)在 android.support.v4.media.session.MediaSessionCompat.(MediaSessionCompat.java:567)在 android.support.v4.media.session.MediaSessionCompat.(MediaSessionCompat.java:537)在 android.support.v4.media.session.MediaSessionCompat.(MediaSessionCompat.java:501)在 android.support.v4.media.session.MediaSessionCompat.(MediaSessionCompat.java:475)在 com.radio.core.service.MediaService.onCreate(MediaService.kt:63)在 android.app.ActivityThread.handleCreateService(ActivityThread.java:4485)... 9 更多
我使用的是最新版本的媒体库(androidx.media:media:1.4.0"),它能够处理来自 AndriodS"的这个要求.正如在 MediaSessionCompact.java 类中看到的那样.
<预><代码>//TODO(b/182513352):使用 PendingIntent.FLAG_MUTABLE 代替 S./*** @隐藏*/@RestrictTo(图书馆)public static final int PENDING_INTENT_FLAG_MUTABLE =Build.VERSION.CODENAME.equals(S") ?0x02000000:0;...if (mbrComponent != null && mbrIntent == null) {//为媒体按钮构造一个 PendingIntentIntent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);//关联的意图将由正在注册的组件处理mediaButtonIntent.setComponent(mbrComponent);mbrIntent = PendingIntent.getBroadcast(上下文,0/* requestCode, 忽略 */, mediaButtonIntent,PENDING_INTENT_FLAG_MUTABLE);}演示问题的源代码 - https://github.com/adelinolobao/issue-媒体会话兼容
你们知道我该如何修复错误吗?
如果你没有在任何地方使用 PendingIntent
.该问题可能会通过添加或更新此依赖项来解决
//需要避免在 Android 12 API 31 上崩溃实现 'androidx.work:work-runtime-ktx:2.7.0'
这解决了我的问题.
I'm trying to update my application to Android SDK 31 but I'm having an issue with MediaSessionCompat.
I have a MediaService that extends the MediaBrowserServiceCompat() and in method onCreate of that service I initialise the MediaSessionCompat.
override fun onCreate() {
super.onCreate()
mediaSession = MediaSessionCompat(this, TAG).apply {
setCallback(mediaSessionCallback)
isActive = true
}
...
But I'm having the following error
java.lang.RuntimeException: Unable to create service com.radio.core.service.MediaService: java.lang.IllegalArgumentException: com.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.
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4498)
at android.app.ActivityThread.access$1500(ActivityThread.java:250)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2064)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7829)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:982)
Caused by: java.lang.IllegalArgumentException: com.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.
at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:567)
at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:537)
at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:501)
at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:475)
at com.radio.core.service.MediaService.onCreate(MediaService.kt:63)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:4485)
... 9 more
I'm using the most recent version of media library ("androidx.media:media:1.4.0") that is able to handle the this requirement from the Andriod "S"". As it's possible to see in the MediaSessionCompact.java class.
// TODO(b/182513352): Use PendingIntent.FLAG_MUTABLE instead from S.
/**
* @hide
*/
@RestrictTo(LIBRARY)
public static final int PENDING_INTENT_FLAG_MUTABLE =
Build.VERSION.CODENAME.equals("S") ? 0x02000000 : 0;
...
if (mbrComponent != null && mbrIntent == null) {
// construct a PendingIntent for the media button
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
// the associated intent will be handled by the component being registered
mediaButtonIntent.setComponent(mbrComponent);
mbrIntent = PendingIntent.getBroadcast(context,
0/* requestCode, ignored */, mediaButtonIntent,
PENDING_INTENT_FLAG_MUTABLE);
}
Source code demonstrating the problem - https://github.com/adelinolobao/issue-media-session-compat
Do you guys have any idea how can I fix the error?
If you are NOT USING PendingIntent
anywhere. The issue might be resolved by adding or updating this dependency
// required to avoid crash on Android 12 API 31
implementation 'androidx.work:work-runtime-ktx:2.7.0'
This fixed my problem.
这篇关于MediaSessionCompat:Targeting S+ (version 31 and above) 要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!