java 以S+(版本31及更高版本)为目标需要指定FLAG_IMMUTABLE或FLAG_MUTABLE之一

uurv41yg  于 2023-01-11  发布在  Java
关注(0)|答案(9)|浏览(378)

App crashes at runtime with the following error :
java.lang.IllegalArgumentException: maa.abc: 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 com.google.android.exoplayer2.ui.PlayerNotificationManager.createBroadcastIntent(PlayerNotificationManager.java:1373) at com.google.android.exoplayer2.ui.PlayerNotificationManager.createPlaybackActions(PlayerNotificationManager.java:1329) at com.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)
I tried all solutions available but the app still crashing on 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);

 }
wko9yo5t

wko9yo5t1#

如果使用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">
tv6aics1

tv6aics12#

检查并更新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);
  }
i2loujxw

i2loujxw3#

在我使用前台交付系统读取标签的情况下,它的工作原理是...
如果您允许应用在Android 12中运行,请使用以下命令:

PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
   pendingIntent = PendingIntent.getActivity(this,
          0, new Intent(this, getClass()).addFlags(
   Intent.FLAG_ACTIVITY_SINGLE_TOP), 
   PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
}else {
   pendingIntent = PendingIntent.getActivity(this,
      0, new Intent(this, getClass()).addFlags(
   Intent.FLAG_ACTIVITY_SINGLE_TOP), 
   PendingIntent.FLAG_UPDATE_CURRENT);
}
dy2hfwbg

dy2hfwbg4#

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)
hgb9j2n6

hgb9j2n65#

我面临着同样的问题,正如大多数答案中所指示的,我已经更新了我的依赖关系,如下所示。

dependencies {
  /* Use either Kotlin or Java dependency as per your project */

  // ...
  /* Android Arch Component Work Manager for Kotlin */
  implementation 'androidx.work:work-runtime-ktx:2.7.0' 

  //...
  /* Android Arch Component Work Manager for Java */
  def work_version = "2.7.1"
  implementation "androidx.work:work-runtime:$work_version"
  // optional - Test helpers
  androidTestImplementation "androidx.work:work-testing:$work_version"
}

但只添加上述内容并没有解决我的问题,应用程序仍然崩溃。
所以我关掉了Android Lint:缺少挂起的Intent可变性,请按以下步骤关闭。

关闭挂起的Intent可变性

转到搜索选项并搜索“PendingIntent”,您将看到如下所示的窗口。

Android Lint的开关:缺少挂起的Intent可变性,您可以开始了。

7fhtutme

7fhtutme6#

我通过在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

jm2pwxwz

jm2pwxwz7#

If theChucklibrary produces your problem, you won't be able to using it after updating the SDK version. You can easily replace it withChucker(Chuck's fork): https://github.com/jgilfelt/chuck/issues/101#issuecomment-869119599.

8ulbf1ek

8ulbf1ek8#

几件事,确保您使用的是JDK 11,其次删除android文件夹中的.gradle文件夹,然后按照这个精彩的答案SO answer
在进行Gradle清洁后尝试

wfveoks0

wfveoks09#

对于react-native项目,可以通过降低目标SDK版本来修复相同的错误。
1.转到安卓/build.gradle
1.在buildscript块的ext中,将targetSDK从31减少到30。

buildscript{
  ext{
    ...
    targetSdkVersion = 30
    ...
  }
  ...
}

相关问题