如何删除所有AlarmManager android

时间:2014-09-19 14:17:44

标签: android android-intent alarmmanager

如何删除android中的所有AlarmManager。我看到答案就像"只需定义一个新的PendingIntent,就像你在创建它时定义的一样。" ,但我没有使相同的pendingIntent,因为在requestCode我使用System.currentTimeMillis()。请帮帮我!

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);

1 个答案:

答案 0 :(得分:1)

我发现如何解决它!我在SharedPrefences中的Set中编写System.currentTimeMillis(),在设置新的Alarm之前我删除旧的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);
    }