应用程序在尝试运行 AlarmManager 和 PendingIntent 时崩溃

问题描述 投票:0回答:1

我正在尝试运行我的应用程序,包含在 AlarmManager 中使用,但程序由于某种原因崩溃了。 具体代码如下:

private void scheduleNotification (Notification notification, int delay)
    {
        Intent notificationIntent = new Intent(this, MyNotificationPublisher.class);
        notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATIONID, 1);
        notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATIONID, notification);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        long futureMillis = SystemClock.elapsedRealtime() + delay;
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        assert alarmManager != null;
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureMillis, pendingIntent);
    }

我在清单中使用,编写了正确的权限,但程序崩溃并出现以下错误:

FATAL EXCEPTION: main
                                                                                                Process: com.example.localnotification, PID: 3172
                                                                                                java.lang.IllegalArgumentException: com.example.localnotification: 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:401)
                                                                                                    at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:671)
                                                                                                    at android.app.PendingIntent.getBroadcast(PendingIntent.java:658)
                                                                                                    at com.example.localnotification.MainActivity.scheduleNotification(MainActivity.java:39)
                                                                                                    at com.example.localnotification.MainActivity.lambda$onCreate$0$com-example-localnotification-MainActivity(MainActivity.java:28)
                                                                                                    at com.example.localnotification.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
                                                                                                    at android.view.View.performClick(View.java:7506)
                                                                                                    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1218)
                                                                                                    at android.view.View.performClickInternal(View.java:7483)
                                                                                                    at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
                                                                                                    at android.view.View$PerformClick.run(View.java:29357)
                                                                                                    at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                    at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                    at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                    at android.os.Looper.loop(Looper.java:288)
                                                                                                    at android.app.ActivityThread.main(ActivityThread.java:7884)
                                                                                                    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:936)

2024-03-02 22:20:35.898 3172-3172 处理 com.example.localnotification

android broadcastreceiver alarmmanager android-pendingintent
1个回答
0
投票

这不是一个明显的事情,如果您的目标是 API 31+,那么它是您待定意图的标志。你可以在docs中查看它,但总之应该是这样的

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE);
© www.soinside.com 2019 - 2024. All rights reserved.