定位 S+(版本 31 及更高版本)需要指定FLAG_IMMUTABLE或FLAG_MUTABLE之一

应用在运行时崩溃,并出现以下错误:

java.lang.IllegalArgumentException: maa.abc: Targeting S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定FLAG_IMMUTABLE或FLAG_MUTABLE之一。强烈建议使用FLAG_IMMUTABLE,仅当某些功能依赖于 PendingIntent 的可变性时才使用FLAG_MUTABLE,例如,如果需要将其与内联回复或气泡一起使用。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 com.google.android.exoplayer2.ui.PlayerNotificationManager.createBroadcastIntent(PlayerNotificationManager.java:1373) at com.google.android.exoplayer2.ui.PlayerNotificationManager.createPlaybackActions(PlayerNotificationManager.java:1329) atcom.google.android.exoplayer2.ui.PlayerNotificationManager.(PlayerNotificationManager.java:643) at com.google.android.exoplayer2.ui.PlayerNotificationManager.(PlayerNotificationManager.java:529) at com.google.android.exoplayer2.ui.PlayerNotificationManager.createWithNotificationChannel(PlayerNotificationManager.java:456) at com.google.android.exoplayer2.ui.PlayerNotificationManager.createWithNotificationChannel(PlayerNotificationManager.java:417)

我尝试了所有可用的解决方案,但该应用程序仍在Android 12上崩溃。

 @Nullable
 @Override
 public PendingIntent createCurrentContentIntent(@NonNull Player player) {
        Intent intent = new Intent(service, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                        Intent.FLAG_ACTIVITY_SINGLE_TOP |
                        Intent.FLAG_ACTIVITY_NEW_TASK);
        return PendingIntent.getActivity(service, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

 }

答案 1

如果使用java或react-native,则将其粘贴到app/build.gradle中

dependencies {
  // ...
  implementation 'androidx.work:work-runtime:2.7.1'
}

如果使用 Kotlin,则使用这个

dependencies {
  // ...
  implementation 'androidx.work:work-runtime-ktx:2.7.0'
}

如果有人仍然面临Android 12的崩溃问题,请确保在AndroidMenifest中添加关注.xml

 <activity 
   ...
   android:exported="true" // in most cases it is true but based on requirements it can be false also   
>   


   // If using react-native push notifications then make sure to add into it also

 <receiver   
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver" android:exported="true">
 
   //  Similarly
 
 <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService" android:exported="true">

答案 2

检查 exoplayer 的依赖版本并将其更新到最新版本

android.app.PendingIntent.getBroadcast()以前用于返回

@Nullable
@Override
private static PendingIntent createBroadcastIntent(
    String action, Context context, int instanceId) {
    Intent intent = new Intent(action).setPackage(context.getPackageName());
    intent.putExtra(EXTRA_INSTANCE_ID, instanceId);
    return PendingIntent.getBroadcast(
        context, instanceId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  }

如果您仔细观察上面的代码段中缺少PendingIntent.FLAG_IMMUTABLE

它现在已更新以返回以下内容

@Nullable
@Override
private static PendingIntent createBroadcastIntent(
      String action, Context context, int instanceId) {
    Intent intent = new Intent(action).setPackage(context.getPackageName());
    intent.putExtra(EXTRA_INSTANCE_ID, instanceId);

    int pendingFlags;
    if (Util.SDK_INT >= 23) {
      pendingFlags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
    } else {
      pendingFlags = PendingIntent.FLAG_UPDATE_CURRENT;
    }

    return PendingIntent.getBroadcast(context, instanceId, intent, pendingFlags);
  }

推荐