【问题标题】:Main Activity fails to start up due to PendingIntent Flag error由于 PendingIntent Flag 错误,Main Activity 无法启动
【发布时间】:2021-12-07 02:30:15
【问题描述】:

我正在开发一个系统应用程序,当我尝试从 Android 31 的主屏幕启动它时,它失败并抛出了 java.lang.IllegalArgumentException。堆栈跟踪如下所示:

java.lang.IllegalArgumentException: tech.[brand].[name]: 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.getBroadcastAsUser(PendingIntent.java:645)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
        at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:285)
        at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:158)
        at androidx.work.impl.utils.ForceStopRunnable.forceStopRunnable(ForceStopRunnable.java:185)
        at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:103)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)

堆栈没有到达我的任何源代码,据我所知,到目前为止,我没有在我的代码中调用任何意图,无论是挂起的还是其他的。有谁知道怎么回事?

【问题讨论】:

    标签: java android kotlin


    【解决方案1】:

    堆栈跟踪中的androidx.workWorkManager。根据WorkManager 2.7.0-alpha02

    明确 PendingIntent 可变性,以修复针对 Android 12 时的崩溃问题。

    因此,您应该升级到最新版本 WorkManager 2.7.1,方法是将对该最新版本的显式依赖添加到您的 build.gradle 文件的 dependencies 块中:

    implementation "androidx.work:work-runtime:2.7.1"
    

    【讨论】:

    • 我的设置为 2.6.0。刚刚解决了几个小时的头发拉扯问题。救命稻草!
    • 我正在使用实现“androidx.work:work-runtime:2.7.1”并且应用程序在三星设备上不断崩溃..
    【解决方案2】:

    PendingIntent 是 API 21+ 中的一个标志。所以你可以这样设置它:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    } 
    else {
     PendingIntent.FLAG_UPDATE_CURRENT 
    }
    

    根据文档here,应该选择FLAG_IMMUTABLE。如果某些功能依赖于PendingIntent,请使用FLAG_MUTABLE

    val updatedPendingIntent = PendingIntent.getActivity(
       applicationContext,
       NOTIFICATION_REQUEST_CODE,
       updatedIntent,
       PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
    )
    

    【讨论】:

      猜你喜欢
      • 2013-06-15
      • 2023-03-22
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 2017-05-04
      • 2014-08-10
      相关资源
      最近更新 更多