設置所有PendingIntents我設置的報警是這樣的:安卓:獲取與AlarmManager
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingEvent);
我感興趣的是去除其中先前設置的所有報警,清除它們。
有沒有辦法讓我做到這一點,或獲得當前設置的所有警報,以便我可以手動刪除它們?
設置所有PendingIntents我設置的報警是這樣的:安卓:獲取與AlarmManager
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingEvent);
我感興趣的是去除其中先前設置的所有報警,清除它們。
有沒有辦法讓我做到這一點,或獲得當前設置的所有警報,以便我可以手動刪除它們?
您需要創建掛起的意圖,然後將其取消
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());
}
正確。無法爲AlarmManager查詢現有的PendingIntents。你必須保留對他們的參考。 – Emmanuel 2010-11-30 17:27:40
無需參考。 alarmManager.cancel取消所有這些警告文檔: 刪除任何具有匹配意圖的警報。任何類型的意圖與其匹配的警報(由filterEquals(Intent)定義)將被取消。 – 2010-11-30 17:32:49
您不必繼續引用。只需定義一個新的PendingIntent,就像您在創建它時所定義的一樣。
例如:
如果我創建了一個的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
要取消警報,您不需要重新創建相同的PendingIntent並傳遞相同的請求代碼。
因此,我創建了一個AlarmUtits來幫助我將所有請求代碼保存在首選項中,以便在將來需要時取消它。您只需要使用以下類別並調用以下方法:
addAlarm
添加新警報並傳遞請求代碼。cancelAlarm
要除去報警,需要重新創建相同的hasAlarm
若要驗證是否添加了該警報,則需要重新創建 相同的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();
}
}
這傢伙演示瞭如何列出當前報警: http://stackoverflow.com/questions/6522792/get-list-of-積極等待警報人員 – slott 2012-01-14 15:21:14