无法启动活动:java.lang.IllegalArgumentException:针对S+(版本31及以上)的目标需要FLAG_IMMUTABLE或FLAG_MUTABLE中的一个。

4
今天我的手机更新到了Android 12系统,我打开一个应用程序时,它开始崩溃。该应用程序在启动时应该发送通知。
val intent = Intent(this, MainActivity::class.java)
        val pendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
            addNextIntentWithParentStack(intent)
            getPendingIntent(INTENT_REQUEST, PendingIntent.FLAG_IMMUTABLE) //Used to be: FLAG_UPDATE_CURRENT
        }

        //Configurar llamada desde boton de notificacion
        val buttonIntent = Intent(this, LlamadaNotificacion::class.java)
        buttonIntent.putExtra("LLAMADA", "LLAMAR")

        val buttonPendingIntent: PendingIntent =
            PendingIntent.getActivity(this, BUTTON_INTENT_REQUEST, buttonIntent, PendingIntent.FLAG_ONE_SHOT)

        val boton = NotificationCompat.Action.Builder(
            R.drawable.dry,"Presiona para realizar llamada de emergencia", buttonPendingIntent
        ).build()

        val notification = NotificationCompat.Builder(this,channelID).also {
            it.setContentTitle("Daira Mayari Mendez Gutierrez")
            it.setContentText("Presiona para más información")
            it.setSmallIcon(R.mipmap.ic_logo)
            //it.setPriority(NotificationCompat.PRIORITY_HIGH)
            it.setContentIntent(pendingIntent)
            it.setVisibility(VISIBILITY_PUBLIC)
            it.addAction(boton)
            it.setAutoCancel(false)
        }.build()

        val notificationManager = NotificationManagerCompat.from(this)

        notificationManager.notify(notificationId,notification)

我已经尝试使用可变和不可变标记,但是调试器会在"PendingIntent.getActivity"行显示错误,如下所示。

这是为我患病的姐姐准备的紧急应用程序,所以我非常感谢您的帮助。谢谢。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.informacion, PID: 11671
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.informacion/com.example.informacion.MainActivity}: java.lang.IllegalArgumentException: com.example.informacion: 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.performLaunchActivity(ActivityThread.java:4037)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4203)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2440)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8641)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)
     Caused by: java.lang.IllegalArgumentException: com.example.informacion: 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:382)
        at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:465)
        at android.app.PendingIntent.getActivity(PendingIntent.java:451)
        at android.app.PendingIntent.getActivity(PendingIntent.java:415)
        at com.example.informacion.MainActivity.onCreate(MainActivity.kt:88)
        at android.app.Activity.performCreate(Activity.java:8282)
        at android.app.Activity.performCreate(Activity.java:8262)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4011)
            ... 12 more

1
请查看:https://dev59.com/Y1EG5IYBdhLWcg3wX7z1#71108334 - Nikunj
2个回答

1
你需要做这个:
    val buttonPendingIntent: PendingIntent =
        PendingIntent.getActivity(this, BUTTON_INTENT_REQUEST, buttonIntent, PendingIntent.FLAG_ONE_SHOT)

你还需要在这个里面添加FLAG_IMMUTABLE或者FLAG_MUTABLE


0
你需要做的是: 在你的 build.gradle 文件中添加以下内容:
def work_version = "2.7.1"
    implementation "androidx.work:work-runtime:$work_version"
    // optional - Test helpers
    androidTestImplementation "androidx.work:work-testing:$work_version"

然后

进入编辑> 查找 > 在文件中查找,搜索 PendingIntent,如果您正在使用 Java 代码,请添加 |PendingIntent.FLAG_IMMUTABLE,如果是 Kotlin,则添加 or PendingIntent.FLAG_IMMUTABLE

enter image description here

您的问题将会被100%解决: 加油!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接