当我将目标SDK更新到30+ (Android R或更高版本)时,我的PendingIntent上出现了一个lint警告Missing PendingIntent可变性标志。当我想定义PendingIntent时,FLAG_UPDATE_CURRENT标志。
我应该如何处理这个绒线没有对应用程序功能的影响?
当我将目标SDK更新到30+ (Android R或更高版本)时,我的PendingIntent上出现了一个lint警告Missing PendingIntent可变性标志。当我想定义PendingIntent时,FLAG_UPDATE_CURRENT标志。
我应该如何处理这个绒线没有对应用程序功能的影响?
当前回答
在我的例子中,它也是由使用旧WorkManager版本的第三方库强制所有依赖的新Android Work版本在你的根构建中使用这个。gradle文件:
allproject {
project.configurations.all {
resolutionStrategy {
force 'androidx.work:work-runtime:2.7.0'
}
}
}
其他回答
您可以将您的未决意图设置为
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是可变的,例如if 它需要与内联回复或气泡一起使用。
相应地选择你的旗帜。
如果你想了解更多,我建议你在这里阅读这篇很棒的文章:https://medium.com/androiddevelopers/all-about-pendingintents-748c8eb8619
下面是我在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"
希望这能起作用,祝你愉快。
来自:
https://developer.android.com/reference/android/app/PendingIntent#FLAG_MUTABLE
"Up until Build.VERSION_CODES.R, PendingIntents are assumed to be mutable by default, unless FLAG_IMMUTABLE is set. Starting with Build.VERSION_CODES.S, it will be required to explicitly specify the mutability of PendingIntents on creation with either (@link #FLAG_IMMUTABLE} or FLAG_MUTABLE. It is strongly recommended to use FLAG_IMMUTABLE when creating a PendingIntent. FLAG_MUTABLE should only be used when some functionality relies on modifying the underlying intent, e.g. any PendingIntent that needs to be used with inline reply or bubbles."
为了保持相同的行为,像每天改变任何“PendingIntent。FLAG_MUTABLE |任何东西"
当创建/检索一个挂起的意图/活动,服务,提供者,接收者
值得寻找的地方:
PendingIntent.getBroadcast(...
.getPendingIntent(...
PendingIntent.getService
PendingIntent.getActivity
如果你的应用程序使用androidx。工作确保至少升级到:
implementation 'androidx.work:work-runtime-ktx:2.7.0-alpha05'
他们在alpha02中修复了一个与SDK 12中所有这些更改相关的错误。
你可以像这样更新挂起的意图:
val updatedPendingIntent = PendingIntent.getActivity(
context,
NOTIFICATION_REQUEST_CODE,
updatedIntent,
PendingIntent.FLAG_IMMUTABLE| PendingIntent.FLAG_UPDATE_CURRENT
)
你可以添加PendingIntent。FLAG_IMMUTABLE加上|符号,它就可以工作了。
我创建了PendingIntentCompat。将PendingIntent逻辑抽象到一个单独的类中。
object PendingIntentCompat {
@JvmStatic
@JvmOverloads
fun getActivity(
context: Context,
requestCode: Int,
intent: Intent,
flags: Int,
isMutable: Boolean = false
): PendingIntent {
return PendingIntent.getActivity(
context,
requestCode,
intent,
addMutabilityFlags(isMutable, flags)
)
}
@JvmStatic
@JvmOverloads
fun getService(
context: Context,
requestCode: Int,
intent: Intent,
flags: Int,
isMutable: Boolean = false
): PendingIntent {
return PendingIntent.getService(
context,
requestCode,
intent,
addMutabilityFlags(isMutable, flags)
)
}
@JvmStatic
@JvmOverloads
@RequiresApi(Build.VERSION_CODES.O)
fun getForegroundService(
context: Context,
requestCode: Int,
intent: Intent,
flags: Int,
isMutable: Boolean = false
): PendingIntent {
return PendingIntent.getForegroundService(
context,
requestCode,
intent,
addMutabilityFlags(isMutable, flags)
)
}
/**
* https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability
*/
private fun addMutabilityFlags(isMutable: Boolean, flags: Int): Int {
var updatedFlags = flags
if (isMutable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
updatedFlags = flags or PendingIntent.FLAG_MUTABLE
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
updatedFlags = flags or PendingIntent.FLAG_IMMUTABLE
}
}
return updatedFlags
}
}