Android:获取所有使用AlarmManager设置的PendingIntents
我正在设置这样的闹钟:
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingEvent);
我有兴趣去除之前设置的所有警报,清除它们。
有没有办法让我这样做,或得到所有目前设置的警报,以便我可以手动删除它们?
您需要创build您的待定意图,然后取消它
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()); }
你不必一定要参考它。 只需要定义一个新的PendingIntent就像您在创build它时所定义的一样。
例如:
如果我创build了一个PendingIntent,可以通过AlarmManager来触发:
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);
然后在你的其他代码的某个地方(甚至是另一个活动),你可以这样做取消:
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);
这里的重要事情是设置PendingIntent完全相同的数据和行动,以及其他标准以及在这里陈述http://developer.android.com/reference/android/app/AlarmManager.html#cancel%28android.app .PendingIntent 29%
要取消警报,您不需要重新创build相同的PendingIntent并传递相同的请求代码。
因此,如果需要的话,我创build了一个AlarmUtits来帮助我将所有请求代码保存在首选项中,以便将来取消它。 所有你需要的是使用下面的类,并调用以下方法:
-
addAlarm
添加一个新的警报并传递一个请求代码。 -
cancelAlarm
删除一个闹钟,你需要重新创build相同的
意图并传递相同的请求代码。 -
hasAlarm
validation是否添加了该警报,则需要重新创build相同的Intent并传递相同的请求代码。 -
cancelAllAlarms
删除设置的所有警报。
我的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(); }
}