一旦我将目标SDK更新为30+ (Android或更高版本),当我想定义PendingIntent
时,PendingIntent.FLAG_UPDATE_CURRENT
标志上就会出现一个林特警告Missing PendingIntent mutability flag
。
我应该如何处理这个不影响应用程序功能的皮棉?
发布于 2021-04-11 14:45:19
您可以将挂起的意图设置为
val updatedPendingIntent = PendingIntent.getActivity(
applicationContext,
NOTIFICATION_REQUEST_CODE,
updatedIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag
)
根据这里的文档:https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability
强烈考虑使用FLAG_IMMUTABLE,只有在某些功能依赖于可变的PendingIntent时才使用FLAG_MUTABLE,例如,如果它需要与内联回复或气泡一起使用。
相应地选择您的国旗。
如果你想读更多关于这一点的文章,我建议你在这里阅读这篇伟大的文章:https://medium.com/androiddevelopers/all-about-pendingintents-748c8eb8619
发布于 2021-04-20 15:06:22
如果您不使用最新版本的WorkManager,您将看到这个问题。它已经在2.7.0-alpha02版本中被修复了
使PendingIntent可变性变得显式,以修复针对Android 12的崩溃
请记住,2.7.0-alpha 02只与Android 12开发者预览1 SDK兼容。所以你可能想要等到它到达测试版或RC。
更新2021年4月21日 --为搜索此问题的任何人添加此答案,您可能遇到的bug可能如下所示:
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就能看到这个崩溃。
正如概述的这里一样,解决方案是为Android12Builds在build.gradle
文件中添加一个依赖项:
implementation 'androidx.work:work-runtime-ktx:2.7.0-alpha05'
注意,无论您只使用Java、Kotlin + coroutines、RxJava2、GCMNetworkManager等等,这种依赖都是不同的。因此,请务必检查上面的dox .。
显然,用最新版本替换上面的版本号。正如前面提到的,它不兼容之前的android-13版本。
发布于 2021-12-04 16:00:29
如果你让你的应用程序运行在安卓12,有一个新的PendingIntent可变标志。如果不希望PendingIntent发生变异,请使用
PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}else {
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
如果您希望您的PendingIntent发生变异,请使用以下命令:
PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
}else {
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
在谷歌文档中,强烈考虑使用FLAG_IMMUTABLE,只有在某些功能依赖于可变的PendingIntent时才使用FLAG_MUTABLE。改变应该是直接的。此外,如果在应用程序中使用AdMob 20.4.0或更低版本,请确保添加以下工作管理器依赖项:
//Work Manager dependency
implementation 'androidx.work:work-runtime:2.7.1'
注意,当前的工作管理器依赖版本是2.7.1。如果需要,可以将该版本更新为最新版本。
https://stackoverflow.com/questions/67045607
复制相似问题