当我将目标SDK更新到30+ (Android R或更高版本)时,我的PendingIntent上出现了一个lint警告Missing PendingIntent可变性标志。当我想定义PendingIntent时,FLAG_UPDATE_CURRENT标志。

我应该如何处理这个绒线没有对应用程序功能的影响?


当前回答

你可以像这样更新挂起的意图:

val updatedPendingIntent = PendingIntent.getActivity(
   context,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE| PendingIntent.FLAG_UPDATE_CURRENT 
)

你可以添加PendingIntent。FLAG_IMMUTABLE加上|符号,它就可以工作了。

其他回答

在我的项目中,这条线是可行的

PendingIntent = PendingIntent。getActivity(this,0, notificationIntent, PendingIntent。| PendingIntent.FLAG_IMMUTABLE;

我将我的work-runtime-ktx版本更新到2.7.1

在上面的修改之后,我又遇到了另一个错误

java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type: ErrorScope{Error scope for class <ERROR CLASS> with arguments: org.jetbrains.kotlin.types.IndexedParametersSubstitution@14ac19e7}

看看我是如何通过更新kotlin-gradle-plugin版本来解决上述错误的。

当你升级你的项目和目标android版本12时,这个问题就来了。使用该解决方案,您可以更新所有待处理实习生

目标S+(版本31及以上)需要在创建PendingIntent时指定FLAG_IMMUTABLE或FLAG_MUTABLE之一

以下代码

 PendingIntent pendingIntent = null;
        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.S){
             pendingIntent = stackBuilder.getPendingIntent(1, PendingIntent.FLAG_MUTABLE);

        }else {
             pendingIntent = stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);

        }

如果你在项目中使用接收器,也要实现这个依赖工作

//Work Manager dependency
implementation 'androidx.work:work-runtime:2.7.1'

这是Work库的问题。 即使是最新版本也受到2.7.0-alpha04的影响

https://issuetracker.google.com/issues/194108978

作为临时解决方案-在gradle中注释掉包含“work”依赖项,并在整个项目中删除使用该类。至少这样你可以正常运行应用程序,并在其他功能和领域工作....

如果你让你的应用在android 12上运行,有一个新的PendingIntent可变标志。如果你不希望你的PendingIntent被静音,使用

Java

PendingIntent updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

科特林

val updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

如果你想要你的PendingIntent静音使用以下方法:

Java

PendingIntent updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

科特林

val updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

最后实现这个依赖

//Work Manager dependency
implementation 'androidx.work:work-runtime:2.7.1'