当我将目标SDK更新到30+ (Android R或更高版本)时,我的PendingIntent上出现了一个lint警告Missing PendingIntent可变性标志。当我想定义PendingIntent时,FLAG_UPDATE_CURRENT标志。
我应该如何处理这个绒线没有对应用程序功能的影响?
当我将目标SDK更新到30+ (Android R或更高版本)时,我的PendingIntent上出现了一个lint警告Missing PendingIntent可变性标志。当我想定义PendingIntent时,FLAG_UPDATE_CURRENT标志。
我应该如何处理这个绒线没有对应用程序功能的影响?
当前回答
我遇到过致命异常:java.lang.IllegalArgumentException这样的崩溃。不发布。附加到带有远程输入的操作的PendingIntents必须是可变的。
我写了这个util方法,它允许将可变性作为参数发送。有时需要获取可变标志,例如通知中的回复操作。
private fun getPendingIntentFlags(isMutable: Boolean = false) =
when {
isMutable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ->
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
!isMutable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ->
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
else -> PendingIntent.FLAG_UPDATE_CURRENT
}
使用的例子:
val quickReplyPendingIntent = PendingIntent.getBroadcast(
context, notificationId, replyIntent,
getPendingIntentFlags(true)
)
其他回答
如果您没有使用最新版本的WorkManager,就会看到这个问题。在2.7.0-alpha02版本中已修复:
使PendingIntent可变显式,以修复针对Android 12时的崩溃
请记住,2.7.0-alpha02只与Android 12开发者预览1 SDK兼容。所以你可能想要等到它进入beta版或RC版。
2021年4月21日更新-为任何谷歌搜索该问题的人添加这个答案,您可能遇到的错误可能是这样的:
java.lang.IllegalArgumentException: com.myapp.myapp: 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.
at android.app.PendingIntent.checkFlags(PendingIntent.java:386)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:657)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:644)
at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:174)
at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:108)
at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:86)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
你不需要在你的应用中直接使用WorkManager来查看这个崩溃。
如本文所述,解决方案是向构建中添加依赖项。Android 12版本的gradle文件:
implementation 'androidx.work:work-runtime-ktx:2.7.0-alpha05'
注意,无论你使用的是Java、Kotlin +协程、RxJava2、GCMNetworkManager等等,这种依赖关系都是不同的。所以一定要检查上面的还原。
显然,用最新的版本号替换上面的版本号。如前所述,它不兼容android-13之前的版本。
由于我的代码中有四个不同的PendingIntents,我首先将FLAG_IMMUTABLE添加到它们中。然而问题仍然存在。 在花了大量时间分析我的4个意图后,我意识到问题可能来自我的一个库。
在构建。gradle,库通常在旧的时候高亮显示,但Firebase的BOM不是这样的。
我有:
implementation platform('com.google.firebase:firebase-bom:26.1.1')
事实证明这是非常古老的。更新到
implementation platform('com.google.firebase:firebase-bom:29.0.4')
一切都很好。没有更多的FLAG_IMMUTABLE错误
你可以像这样更新挂起的意图:
val updatedPendingIntent = PendingIntent.getActivity(
context,
NOTIFICATION_REQUEST_CODE,
updatedIntent,
PendingIntent.FLAG_IMMUTABLE| PendingIntent.FLAG_UPDATE_CURRENT
)
你可以添加PendingIntent。FLAG_IMMUTABLE加上|符号,它就可以工作了。
我想分享一下我的病例。我把标志改为FLAG_IMMUTABLE,但只有当应用程序在后台时才会出现错误。我从这里解决了:https://github.com/firebase/firebase-android-sdk/issues/3115
根本原因是因为我以不赞成的方式检索FCM令牌:
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(activity, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String newToken = instanceIdResult.getToken();
}
});
然后更新依赖项:
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(task -> {
String newToken = task.getResult();
});
final int flag = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_UPDATE_CURRENT;
PendingIntent pendingIntent = PendingIntent.getActivity(context, PENDING_INTENT_REQUEST_CODE, notificationIntent, flag);