当前位置: 首页 > news >正文

解决使用PendingIntent.getBroadcast时出现java.lang.IllegalArgumentException异常的问题

当app为targetSdk31及以上,并且在Android12及以上系统中调用PendingIntent.getBroadcast(context, 0, intent, 0)接口时会抛出异常:

java.lang.IllegalArgumentException: com.haier.uhome.uplus.seasia: 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.FLAG_IMMUTABLE或PendingIntent.FLAG_MUTABLE:

PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)

或:

PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_MUTABLE)

参数解释:
PendingIntent.FLAG_IMMUTABLE:创建一个不可变的 PendingIntent。这意味着其他应用无法通过 send() 方法中的额外 Intent 参数来修改 PendingIntent 中的 Intent。
PendingIntent.FLAG_MUTABLE:创建一个可变的 PendingIntent。这意味着其他应用可以通过 send() 方法中的额外 Intent 参数来填充或修改 PendingIntent 中的 Intent。
推荐使用PendingIntent.FLAG_IMMUTABLE,因为此方式创建的PendingIntent更安全。

http://www.dtcms.com/a/118570.html

相关文章:

  • (四十)Dart 中的空安全(Null Safety)教程
  • Web品质 - 重要的HTML元素
  • Linux 命令清单(Linux Command List)
  • MySQL随机获取记录之方法(The Method of Randomly Obtaining Records in MySQL)
  • 【python3】关于等额本金和等额本息计算
  • Activiti(二)- 基于SpringBoot开发配置activiti相关配置项
  • 深入理解C++面向对象特性之一 多态
  • Linux驱动开发进阶(六)- 多线程与并发
  • Redis到底能不能做主数据库?
  • xv6-labs-2024 lab1
  • QML面试笔记--UI设计篇03导航控件
  • 国内数据安全传送简述
  • python 微信小程序支付、查询、退款使用wechatpy库
  • 神经探针与价值蓝海:AI重构需求挖掘的认知拓扑学
  • 深度学习 Deep Learning 第19章 近似推理
  • 基于SpringBoot的在线拍卖系统(源码+数据库+万字文档+ppt)
  • 【LeetCode 题解】算法:34.在排序数组中查找元素的第一个和最后一个位置
  • Kafka 中的 offset 提交问题
  • Qt 资源文件(.qrc 文件)
  • 基于SpringBoot的“高校社团管理系统”的设计与实现(源码+数据库+文档+PPT)
  • 基于ensp的mpls的解决bgp域内黑洞及MPLS VPN的应用
  • 心脏滴血漏洞(CVE-2014-0160)漏洞复现
  • 探秘PythonJSON解析深度剖析json.loads处理嵌套JSON字符串的奥秘
  • 《UNIX网络编程卷1:套接字联网API》第3章 套接字编程简介
  • MBR的 扩展分区 和 逻辑分区 笔记250407
  • 循环神经网络 - 机器学习任务之同步的序列到序列模式
  • 计算机网络学习前言
  • 八、C++速通秘籍—动态多态(运行期)
  • 【蓝桥杯】搜索算法:剪枝技巧+记忆化搜索
  • SpringBoot类跨包扫描失效的几种解决方法