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

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


当前回答

由于我的代码中有四个不同的PendingIntents,我首先将FLAG_IMMUTABLE添加到它们中。然而问题仍然存在。 在花了大量时间分析我的4个意图后,我意识到问题可能来自我的一个库。

在构建。gradle,库通常在旧的时候高亮显示,但Firebase的BOM不是这样的。

我有:

implementation platform('com.google.firebase:firebase-bom:26.1.1')

事实证明这是非常古老的。更新到

implementation platform('com.google.firebase:firebase-bom:29.0.4')

一切都很好。没有更多的FLAG_IMMUTABLE错误

其他回答

如果你让你的应用在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'

如果你使用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'
        }
    }

实现androidx.work:work-runtime:2.7.1

下面是我在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"

希望这能起作用,祝你愉快。

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

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