Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified

简介: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified


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

java.lang.IllegalArgumentException: com.example.imdemo: 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.

错误分析:

错误提示在使用PendingIntent时,无论是创建或使用的时候,flags参数要求必须添加 FLAG_IMMUTABLE或者FLAG_MUTABLE的两个之中其中一个,而且官方还强烈推荐使用FLAG_IMMUTABLE(代码不同,依情况而定官方的推荐)。

解决方法

1.将项目的targetSdkVersion由31改为30

2.如果不想改targetSdkVersion,那就在在创建PendingIntent的时候判断当前系统版本,根据不同系统版本创建带有不同flag的PendingIntent,具体代码实现如下:

PendingIntent 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);
}


目录
相关文章
|
19天前
|
小程序 JavaScript
Avoid mutating a prop directly since the value will be overwritten whenever the parent comp
Avoid mutating a prop directly since the value will be overwritten whenever the parent comp
|
11月前
|
JavaScript
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent .(一)
大致意思就是props接收到的从父组件传过来的tableData不能直接修改。
|
11月前
|
JavaScript
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent .(二)
1.在data中声明一个变量接收props的值,然后再去改变data里的这个值 2. 用computed属性 3.用data保存数据,watch监听
|
NoSQL MongoDB 数据库
DeprecationWarning: count is deprecated. Use Collection.count_documents instead
当我使用pymongo查询出对应的cursor(find出的document的迭代器),然后查看查询出数据的数量时使用如下代码: ```python db = MongoClient(host='192.168.1.3', port=27017) # dbname为操作的数据库名称,collectionname为操作的集合名称
271 0
The 'manifest_version' key must be present and set to 2 (without quotes)
The 'manifest_version' key must be present and set to 2 (without quotes)
67 0
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
解决办法:Type safety: The expression of type List needs unchecked conversion to conform
215 0
|
Android开发 Kotlin
【错误记录】Kotlin 编译报错 ( Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable ... )
【错误记录】Kotlin 编译报错 ( Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable ... )
485 0
【错误记录】Kotlin 编译报错 ( Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable ... )
Changing scope values asynchronously - updates don't propagate without .apply()
Changing scope values asynchronously - updates don't propagate without .apply()
Changing scope values asynchronously - updates don't propagate without .apply()
Changing scope values asynchronously - updates don't propagate without .apply()
Changing scope values asynchronously - updates don't propagate without .apply()
Changing scope values asynchronously - updates don't propagate without .apply()