Android: set all PendingIntents using AlarmManager

I set the alarm as follows:

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

I am interested in deleting all alarms that were set earlier and clearing them.

Is there a way to do this or get all the currently set alarms so that I can manually remove them?

+62
android alarmmanager
Nov 30 '10 at 15:50
source share
3 answers

You need to create your pending intent and then cancel it

  AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent updateServiceIntent = new Intent(context, MyPendingIntentService.class); PendingIntent pendingUpdateIntent = PendingIntent.getService(context, 0, updateServiceIntent, 0); // Cancel alarms try { alarmManager.cancel(pendingUpdateIntent); } catch (Exception e) { Log.e(TAG, "AlarmManager update was not canceled. " + e.toString()); } 
+57
Nov 30 '10 at 16:51
source share

You do not need to refer to it. Just define a new PendingIntent, like the one you defined when you created it.

For example:

if I created a PendingIntent that AlarmManager should be started as follows:

  Intent alarmIntent = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class); alarmIntent.setData(Uri.parse("custom://" + alarm.ID)); alarmIntent.setAction(String.valueOf(alarm.ID)); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); PendingIntent displayIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0); alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDateTime, displayIntent); 

Then somewhere in another code (even in a different action) you can do this to undo:

 Intent alarmIntent = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class); alarmIntent.setData(Uri.parse("custom://" + alarm.ID)); alarmIntent.setAction(String.valueOf(alarm.ID)); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); PendingIntent displayIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0); alarmManager.cancel(displayIntent); 

It is important here to set up a PendingIntent with exactly the same data and action and other criteria, as well as the http://developer.android.com/reference/android/app/AlarmManager.html#cancel%28android.app.PendingIntent%29 listed here

+76
Feb 02 '12 at 11:12
source share

To cancel the alarm, you do not need to recreate the same PendingIntent and pass the same request code.

So, I created AlarmUtits to help me save all request codes in the settings, to cancel it in the future, if necessary. All you need to do is use the following class and call the following methods:

  • addAlarm to add a new signal and pass the request code.
  • cancelAlarm , in order to delete the alarm, you need to recreate the same file. Configure and send the same request code.
  • hasAlarm , to check if this alarm is added, you need to recreate the same Intent and pass the same request code.
  • cancelAllAlarms to remove ALL installed alarms.

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<Integer> idsAlarms = getAlarmIds(context); if (idsAlarms.contains(id)) { return; } idsAlarms.add(id); saveIdsInPreferences(context, idsAlarms); } private static void removeAlarmId(Context context, int id) { List<Integer> 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<Integer> getAlarmIds(Context context) { List<Integer> 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<Integer> 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(); } 

}

+6
Feb 15 '17 at 1:47
source share



All Articles