当前位置: 首页 > 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更安全。

相关文章:

  • (四十)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)
  • 龚正会见哥伦比亚总统佩特罗
  • 特朗普促卡塔尔说服伊朗放弃核计划,伊朗总统:你来吓唬我们?
  • 马上评|安排见义勇为学生补考,善意与善意的双向奔赴
  • “80后”德州市接待事务中心副主任刘巍“拟进一步使用”
  • 220名“特朗普币”持有者花1.48亿美元,获邀与特朗普共进晚餐
  • 联合国秘书长欢迎中美经贸高层会谈成果