Search code examples
androidandroid-intentalarmmanager

How delete all AlarmManager android


How delete all AlarmManager in android. I see answer like "Just define a new PendingIntent like exactly the one that you defined in creating it." , but i did not make same pendingIntent because in requestCode i use System.currentTimeMillis(). Help me please!

Intent intent = new Intent(this, ShowNotificationService.class);
PendingIntent pIntent = PendingIntent.getService(this, (int) System.currentTimeMillis(), intent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);

Solution

  • I find how fix it! I write System.currentTimeMillis() in Set in SharedPrefences, and before set new Alarm i delete older Alarm!

    Set<String> unicTimesSet = sp.getStringSet(cons.UNIC_TIMES_SET, new HashSet<String>());
        for (String unicTime : unicTimesSet) {
            Intent intentShowNotif = new Intent(this, ShowNotificationService.class);
            PendingIntent pIntent = PendingIntent.getService(this, (int)Long.parseLong(unicTime), intentShowNotif, 0);
            manager.cancel(pIntent);
        }