【问题标题】:Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified定位 S+(版本 31 及更高版本)需要指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一
【发布时间】:2022-01-28 12:59:55
【问题描述】:

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

java.lang.IllegalArgumentException: maa.abc: Targeting S+(版本 31 及更高版本)要求 FLAG_IMMUTABLE 或 创建 PendingIntent 时指定 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) 在 com.google.android.exoplayer2.ui.PlayerNotificationManager.createBroadcastIntent(PlayerNotificationManager.java:1373) 在 com.google.android.exoplayer2.ui.PlayerNotificationManager.createPlaybackActions(PlayerNotificationManager.java:1329) 在 com.google.android.exoplayer2.ui.PlayerNotificationManager.(PlayerNotificationManager.java:643) 在 com.google.android.exoplayer2.ui.PlayerNotificationManager.(PlayerNotificationManager.java:529) 在 com.google.android.exoplayer2.ui.PlayerNotificationManager.createWithNotificationChannel(PlayerNotificationManager.java:456) 在 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);

 }

【问题讨论】:

    标签: java android android-pendingintent exoplayer2.x android-12


    【解决方案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);
        }
      

      如果你仔细观察上面的sn-p中这里缺少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);
        }
      

      【讨论】:

      • 很酷,但是当错误信息是“Targeting S+ (version 31 and above)”时,我们为什么要检查版本 23 和更高版本
      • @Tarek PendingIntent.FLAG_IMMUTABLE 在低于 23 的 API 中不存在。错误消息是因为 android 已强制将 PendingIntent.FLAG_IMMUTABLE 或 PendingIntent.FLAG_IMUTABLE 添加到来自 API 31 的所有待处理意图(S ) 以后
      【解决方案3】:

      Kotlin 的解决方案,如果您使用 API M,只需添加此标志

      val flags = when {
                  Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
                  else -> FLAG_UPDATE_CURRENT
              }
      val toastPendingIntent = PendingIntent.getBroadcast(context, 0, providerIntent, flags)
      

      【讨论】:

        【解决方案4】:

        我通过在 ...android/app/build.gradle 中添加以下内容解决了这个问题

        implementation 'androidx.work:work-runtime-ktx:2.8.0-alpha01'
        

        https://github.com/react-native-maps/react-native-maps/issues/4083#issue-1119280606

        【讨论】:

          猜你喜欢
          • 2022-08-22
          • 1970-01-01
          • 2022-10-05
          • 2022-01-08
          • 2022-07-09
          • 2022-07-22
          • 1970-01-01
          • 2021-09-29
          • 2023-01-01
          相关资源
          最近更新 更多