今天我的手机更新到了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