gpt4 book ai didi

android - 缺少可变性标志 : Android 12 pending intents with NavDeepLinkBuilder

转载 作者:行者123 更新时间:2023-12-04 23:39:32 36 4
gpt4 key购买 nike

我已经开始在 Android 12 上测试我的应用程序的问题,并且收到了一些关于为主屏幕小部件设置的待处理 Intent 的可变性标志的警告。这些可变性标志现在在 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)
在 Android 12 上,需要添加第二个标志,如下所示:
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)
到目前为止,一切都很好。但是,对于某些小部件操作,不仅需要导航到应用程序,还需要导航到特定 fragment 并添加 fragment 参数。对于这些情况,我一直在使用 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.access$1600(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 构建挂起的 Intent 时设置强制可变性标志?我在 NavDeepLinkBuilder 的文档中找不到任何相关信息。可以像在第二个代码 fragment 中那样设置挂起的 Intent ,我可以使用 Intent 操作或额外的来设置“地址”以到达所需的 fragment ,但我失去了向 fragment 本身发送 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)。

关于android - 缺少可变性标志 : Android 12 pending intents with NavDeepLinkBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69829026/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com