当我将目标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和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和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'
}
}
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);
如果你让你的应用在android 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。如果您愿意,可以将版本更新到最新版本。
在我的项目中,这条线是可行的
PendingIntent = PendingIntent。getActivity(this,0, notificationIntent, PendingIntent。| 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
}
}