Android 12 行为变更:适配以Android 12为目标的应用,经验分享

本文详细介绍了Android 12中针对应用的重要行为变更,包括服务必须明确声明`android:exported`,PendingIntent需指定可变性,避免不安全的嵌套intent启动,后台应用无法启动前台服务,以及通知trampoline限制。开发者需要更新代码以适应这些新规定,以确保应用在Android 12上的正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果您的应用在需要声明 android:exported 的值时未进行此声明,则 Logcat 会提供以下错误消息:

Targeting S+ (version 10000 and above) requires that an explicit value for \

android:exported be defined when intent filters are present

以下代码段显示了一个服务示例,该服务包含 intent 过滤器并针对 Android 12 进行了正确配置:

<service android:name=“com.example.app.backgroundService”

android:exported=“false”>

PendingIntent 必须声明可变性


如果以 Android 12 为目标平台,必须为应用创建的每个 PendingIntent 对象指定可变性。

要声明特定 PendingIntent 对象是否可变,请分别使用 PendingIntent.FLAG_MUTABLEPendingIntent.FLAG_IMMUTABLE 标志。

如果您的应用创建未包含设置任何可变标志的 PendingIntent 对象,系统会抛出 IllegalArgumentException,并在 Logcat 中显示以下消息:

PACKAGE_NAME: Targeting S+ (version 10000 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 对象:

  • 通知中的直接回复操作需要变更与回复关联的 PendingIntent 对象中的剪辑数据。通常,您可以通过将 FILL_IN_CLIP_DATA 作为标志传递给 fillIn() 的方法请求此变更。

  • 如果您的应用使用 PendingIntent 将对话放在气泡中,则 intent 应该可变,以便系统可以应用正确的标志,例如 FLAG_ACTIVITY_MULTIPLE_TASK 和 FLAG_ACTIVITY_NEW_DOC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值