'What flag type should I use to cancel the alarm receive in Android?

I am getting error in MainActivity.alarmReceiverCancel method. the reason is this; In general, the definition of the problem is: alarmReceiver does not cancel

Exception: com.appname: 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.

I searched a lot but couldn't find a solution. because when I use FLAG_IMMUTABLE, when it works for an alarm, my other alarms also work, but I have 7 or 8 alarms. What should I do in this situation?

    @SuppressLint("UnspecifiedImmutableFlag")
    public void alarmReceiverCancel() {
        for (int i = 0; i <= 7; i++) {
            AlarmManager alarmManager =
                    (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(this, AlarmReceiver.class);
            PendingIntent pendingIntent =
                    PendingIntent.getService(this, i, intent, PendingIntent.FLAG_NO_CREATE);
            if (pendingIntent != null && alarmManager != null) {
                alarmManager.cancel(pendingIntent);
            }
        }
    }

onReceive(){


        notificationID = intent.getIntExtra("NotificationID",999);

        if (notificationID == 0) {
   createNotification(context, context.getString(R.string.text2));
            notificationID = 999;
        }
        if (notificationID == 1) {
            createNotification(context, context.getString(R.string.text1));
            notificationID = 999;

        }


}






Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source