缺少可变性标志:Android 12 个与 NavDeepLinkBuilder 挂起的意图

Missing mutability flags: Android 12 pending intents with NavDeepLinkBuilder

我已经开始在 Android 12 上测试我的应用程序是否存在问题,并且收到了一些关于为主屏幕小部件设置的未决意图上的可变性标志的警告。这些可变性标志现在从 SDK 31 开始是强制性的。在 31 之前,这段代码可以工作,但有一个 lint 警告:

val myIntent = Intent(context, MainActivity::class.java)
val myPendingIntent = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT )
myRemoteViews.setOnClickPendingIntent(R.id.widget_button, myPendingIntent)

关于Android12,需要添加第二个flag,像这样:

val myIntent = Intent(context, MainActivity::class.java)
val myPendingIntent = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
myRemoteViews.setOnClickPendingIntent(R.id.widget_button, myPendingIntent)

到目前为止一切顺利。但是,对于某些小部件操作,不仅需要导航到应用程序,还需要导航到特定片段并添加片段参数。对于这些情况,我一直在使用 NavDeepLinkBuilder 来创建 pendingIntent https://developer.android.google.cn/reference/kotlin/androidx/navigation/NavDeepLinkBuilder

val myPendingIntent = NavDeepLinkBuilder(context)
        .setGraph(R.navigation.nav_main)
        .setDestination(R.id.myFragment)
        .setArguments(
            myFragment.bundle(myArgs)
        )
        .createPendingIntent()
myRemoteViews.setOnClickPendingIntent(R.id.widget_button, myPendingIntent)

在您将构建目标设置为 sdk 31 并在 Android 12(模拟器)上构建之前,这非常有效。在那种情况下,它会在使用此堆栈跟踪创建小部件时崩溃:

   2021-11-03 17:41:27.792 20309-20309/com.package.debug E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.package.debug, PID: 20309
    java.lang.RuntimeException: Unable to start receiver com.package.widgets.mywidget.ui.MyWidgetProvider: java.lang.IllegalArgumentException: com.package.debug: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:4321)
        at android.app.ActivityThread.access00(ActivityThread.java:247)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2068)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.IllegalArgumentException: com.package.debug: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
        at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
        at android.app.PendingIntent.getActivitiesAsUser(PendingIntent.java:593)
        at android.app.PendingIntent.getActivities(PendingIntent.java:575)
        at androidx.core.app.TaskStackBuilder.getPendingIntent(TaskStackBuilder.java:341)
        at androidx.core.app.TaskStackBuilder.getPendingIntent(TaskStackBuilder.java:312)
        at androidx.navigation.NavDeepLinkBuilder.createPendingIntent(NavDeepLinkBuilder.java:260)
        at com.ms1881.widgets.listwidget.ui.ListWidgetProvider.buildRemoteViews(ListWidgetProvider.kt:91)
        at com.ms1881.widgets.listwidget.ui.ListWidgetProvider.updateListWidget(ListWidgetProvider.kt:44)
        at com.ms1881.widgets.listwidget.ui.ListWidgetProvider.onUpdate(ListWidgetProvider.kt:39)
        at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
        at com.ms1881.widgets.listwidget.ui.ListWidgetProvider.onReceive(ListWidgetProvider.kt:169)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:4312)
            ... 9 more

所以问题是:如何在使用 NavDeepLinkBuilder 构建待定意图时设置强制可变性标志?我在 NavDeepLinkBuilder 的文档中找不到任何关于此的信息。可以像在第二个代码片段中一样设置未决意图,我可以使用意图操作或额外设置“地址”以到达所需的片段,但我失去了将 bundle 参数发送到片段本身的可能性.感谢所有有关如何解决此问题的意见。

根据 Navigation 2.4.0-alpha04 release notes:

NavDeepLinkBuilder now adds PendingIntent.FLAG_IMMUTABLE to the PendingIntent returned by createPendingIntent(), ensuring that this API works as expected when targeting Android 12. (If8c52)

如果需要修复,您需要升级到 Navigation 2.4.0-alpha04 或更高版本(当前为 2.4.0-beta01)。