Android: Get all PendingIntents set with AlarmManager

后端 未结 3 1928
傲寒
傲寒 2020-11-27 10:55

I\'m setting an alarm like this:

alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingEvent);

I\'m interested in removing all the al

3条回答
  •  野性不改
    2020-11-27 11:36

    To cancel an alarm you need to re-create the same PendingIntent and pass the same request code.

    So, I have created an AlarmUtils to help me saving all the request codes in the preferences to cancel it in the future if it is needing. All you need is to use the following class and call the following methods:

    • addAlarm to add a new alarm and pass a request code.
    • cancelAlarm to remove an alarm, you need to re-create the same
      Intent and pass the same request code.
    • hasAlarm to verify if that alarm as added, you need to re-create the same Intent and pass the same request code.
    • cancelAllAlarms to remove ALL alarms setted.

    My AlarmUtils

    public class AlarmUtils {
    
        private static final String sTagAlarms = ":alarms";
    
        public static void addAlarm(Context context, Intent intent, int notificationId, Calendar calendar) {
    
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            }
    
            saveAlarmId(context, notificationId);
        }
    
        public static void cancelAlarm(Context context, Intent intent, int notificationId) {
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            alarmManager.cancel(pendingIntent);
            pendingIntent.cancel();
    
            removeAlarmId(context, notificationId);
        }
    
        public static void cancelAllAlarms(Context context, Intent intent) {
            for (int idAlarm : getAlarmIds(context)) {
                cancelAlarm(context, intent, idAlarm);
            }
        }
    
        public static boolean hasAlarm(Context context, Intent intent, int notificationId) {
            return PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_NO_CREATE) != null;
        }
    
        private static void saveAlarmId(Context context, int id) {
            List idsAlarms = getAlarmIds(context);
    
            if (idsAlarms.contains(id)) {
                return;
            }
    
            idsAlarms.add(id);
    
            saveIdsInPreferences(context, idsAlarms);
        }
    
        private static void removeAlarmId(Context context, int id) {
            List idsAlarms = getAlarmIds(context);
    
            for (int i = 0; i < idsAlarms.size(); i++) {
                if (idsAlarms.get(i) == id)
                    idsAlarms.remove(i);
            }
    
            saveIdsInPreferences(context, idsAlarms);
        }
    
        private static List getAlarmIds(Context context) {
            List ids = new ArrayList<>();
            try {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                JSONArray jsonArray2 = new JSONArray(prefs.getString(context.getPackageName() + sTagAlarms, "[]"));
    
                for (int i = 0; i < jsonArray2.length(); i++) {
                    ids.add(jsonArray2.getInt(i));
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return ids;
        }
    
        private static void saveIdsInPreferences(Context context, List lstIds) {
            JSONArray jsonArray = new JSONArray();
            for (Integer idAlarm : lstIds) {
                jsonArray.put(idAlarm);
            }
    
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString(context.getPackageName() + sTagAlarms, jsonArray.toString());
    
            editor.apply();
        }
    }
    

提交回复
热议问题