【已解决】Android12以上PendingIntent需要强制增加FLAG_IMMUTABLE或FLAG_MUTABLE

在升级到Android13时,出现了一个错误,指明从版本31及以上要求在创建PendingIntent时指定FLAG_IMMUTABLE或FLAG_MUTABLE。解决方案是检查代码中PendingIntent的创建方式,对于AndroidS及更高版本,应使用FLAG_IMMUTABLE,否则使用默认值。已提供的代码修改示例展示了如何根据API级别选择正确的标志。

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

Android13适配时报下面错误:

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

02-26 20:00:47.055 27821 27821 I System  : java.lang.IllegalArgumentException: android: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
02-26 20:00:47.055 27821 27821 I System  : 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.
02-26 20:00:47.055 27821 27821 I System  :     at android.app.PendingIntent.checkFlags(PendingIntent.java:404)
02-26 20:00:47.055 27821 27821 I System  :     at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:674)
02-26 20:00:47.055 27821 27821 I System  :     at android.app.PendingIntent.getBroadcast(PendingIntent.java:661)
02-26 20:00:47.055 27821 27821 I System  :     at com.XX.server.ntp.SunmiNetworkTimeUpdateService.<init>(SunmiNetworkTimeUpdateService.java:148)
02-26 20:00:47.055 27821 27821 I System  :     at com.XX.server.SunmiSystemServer.<init>(SunmiSystemServer.java:55)
02-26 20:00:47.055 27821 27821 I System  :     at com.android.server.SystemServer.startOtherServices(SystemServer.java:2414)
02-26 20:00:47.055 27821 27821 I System  :     at com.android.server.SystemServer.run(SystemServer.java:944)
02-26 20:00:47.055 27821 27821 I System  :     at com.android.server.SystemServer.main(SystemServer.java:654)
02-26 20:00:47.055 27821 27821 I System  :     at java.lang.reflect.Method.invoke(Native Method)
02-26 20:00:47.055 27821 27821 I System  :     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
02-26 20:00:47.055 27821 27821 I System  :     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:920)
02-26 20:00:47.055 27821 27821 W SystemServer: ***********************************************
02-26 20:00:47.055 27821 27821 E SystemServer: BOOT FAILURE starting SunmiSystemServer
02-26 20:00:47.055 27821 27821 E SystemServer: java.lang.IllegalArgumentException: android: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
02-26 20:00:47.055 27821 27821 E SystemServer: 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.
02-26 20:00:47.055 27821 27821 E SystemServer:     at android.app.PendingIntent.checkFlags(PendingIntent.java:404)
02-26 20:00:47.055 27821 27821 E SystemServer:     at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:674)
02-26 20:00:47.055 27821 27821 E SystemServer:     at android.app.PendingIntent.getBroadcast(PendingIntent.java:661)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.XX.server.ntp.SunmiNetworkTimeUpdateService.<init>(SunmiNetworkTimeUpdateService.java:148)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.XX.server.SunmiSystemServer.<init>(SunmiSystemServer.java:55)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.android.server.SystemServer.startOtherServices(SystemServer.java:2414)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.android.server.SystemServer.run(SystemServer.java:944)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.android.server.SystemServer.main(SystemServer.java:654)
02-26 20:00:47.055 27821 27821 E SystemServer:     at java.lang.reflect.Method.invoke(Native Method)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
02-26 20:00:47.055 27821 27821 E SystemServer:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:920)
02-26 20:00:47.056 27821 27821 D SystemServerTiming: StartPrintManager

修改方法:

   148         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
   149             mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, PendingIntent.FLAG_IMMUTABLE);
   150         }else{
   151             mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0);
   152         } 

参考:

App和SDK开发必看 | 个推分享Android12适配指南 - 知乎 (zhihu.com)

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值