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

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


当前回答

我创建了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
    }
}

其他回答

由于我的代码中有四个不同的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错误

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

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

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

我想分享一下我的病例。我把标志改为FLAG_IMMUTABLE,但只有当应用程序在后台时才会出现错误。我从这里解决了:https://github.com/firebase/firebase-android-sdk/issues/3115

根本原因是因为我以不赞成的方式检索FCM令牌:

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(activity,  new OnSuccessListener<InstanceIdResult>() {
        @Override
        public void onSuccess(InstanceIdResult instanceIdResult) {
            String newToken = instanceIdResult.getToken();
       
        }
    });

然后更新依赖项:

FirebaseMessaging.getInstance().getToken().addOnCompleteListener(task -> {
            String newToken = task.getResult();
        });

如果你的应用目标是Android 12 (targetSdkVersion = 31),并且直接使用旧版本的WorkManager或任何第三方库,那么你需要将其更新到最新版本来解决它。

dependencies {
    val work_version = "2.7.1"

    // (Java only)
    implementation("androidx.work:work-runtime:$work_version")

    // Kotlin + coroutines
    implementation("androidx.work:work-runtime-ktx:$work_version")

    // optional - RxJava2 support
    implementation("androidx.work:work-rxjava2:$work_version")        
}

来自:

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中所有这些更改相关的错误。