当我将目标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)
)
其他回答
下面是我在KOTLIN中从30移动到33的用例。
1. 添加媒体依赖
implementation "androidx.media:media:1.4.1"
2. 更新工作管理器
implementation "androidx.work:work-runtime-ktx:2.7.0"
3.更新不变的
fun getImmutableFlag() = if(isAndroidAPI31()) PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT else 0
fun isAndroidAPI31() = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S
private fun createOpenAppIntent(context: Context): PendingIntent {
val intent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
}
return PendingIntent.getActivity(context, 0, intent, getImmutableFlag())
}
4. 如果在所有活动、服务、提供者、接收者中未添加,则在清单中添加导出标签
android:exported="true"
希望这能起作用,祝你愉快。
如果你使用Java和ADMOB,你会在SDK S或Android 12上遇到PendingIntent错误。这里有一个修复,使ADMOB使用正确的工作-运行时。
implementation 'com.google.android.gms:play-services-ads:19.5.0'
constraints {
implementation('androidx.work:work-runtime:2.7.0-alpha05') {
because 'previous versions have a bug impacting this application'
}
}
我遇到过致命异常: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)
)
我将我的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版本来解决上述错误的。
这是Work库的问题。 即使是最新版本也受到2.7.0-alpha04的影响
https://issuetracker.google.com/issues/194108978
作为临时解决方案-在gradle中注释掉包含“work”依赖项,并在整个项目中删除使用该类。至少这样你可以正常运行应用程序,并在其他功能和领域工作....