Android12遇到的问题

java.lang.IllegalArgumentException:

Targeting S+ (version 10000 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE

最近在写代码时碰到如下错误:

 java.lang.IllegalArgumentException: com.example.myeyepetizer: Targeting S+ 
 (version 10000 and above) requires that one of FLAG_IMMUTABLE or 
 FLAG_MUTABLE be specified when creating a PendingIntent.
   

查了一番资料后,发现如下几个修复的方法(看到别的博客_):

  1. 将项目的targetSdkVersion由31改为30
  2. 如果不想改targetSdkVersion,那就在在创建PendingIntent的时候判断当前系统版本,根据不同系统版本创建带有不同flag的PendingIntent,具体代码实现如下:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
    pendingIntent = PendingIntent.getActivity(this, 123, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
    pendingIntent = PendingIntent.getActivity(this, 123, intent, PendingIntent.FLAG_ONE_SHOT);
}
  1. 直接在项目中依赖下面的库
dependencies {
  // For Java
  implementation 'androidx.work:work-runtime:2.7.1' 
 
  // For Kotlin
  implementation 'androidx.work:work-runtime-ktx:2.7.1'
}

这里最推荐的是使用上面的方法2。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值