我在App上使用多个闹钟。 每次打开应用程序我设置4警报,但如果设置警报然后我已删除所有警报并再次设置。 设置闹钟时这是我的代码:
public static void setAlarm(Context context, int hour, int minute, boolean addDay, int i) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
Calendar calendar = Calendar.getInstance();
if (addDay) {
calendar.add(Calendar.DATE, 1);
}
int idRequest = Integer.parseInt(hour+""+minute);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
intent.putExtra(TYPE_ALARM, i);
intent.putExtra(HOUR, hour);
intent.putExtra(MINUTE, minute);
intent.putExtra(REQUEST_CODE, (int) calendar.getTimeInMillis());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
idRequest, intent, PendingIntent.FLAG_NO_CREATE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d("Set Alarm", hour + ": " + minute +" "+ calendar.get(Calendar.AM_PM) + calendar.get(Calendar.DAY_OF_MONTH) + "/" +
(calendar.get(Calendar.MONTH) + 1)+" idPedding: "+idRequest);
}
Android删除所有闹钟:
public static void cancleAlarm(Context context, int hour, int minute, int i) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
intent.putExtra(TYPE_ALARM, i);
intent.putExtra(HOUR, hour);
intent.putExtra(MINUTE, minute);
intent.putExtra(REQUEST_CODE, calendar.getTimeInMillis());
int idRequest = Integer.parseInt(hour+""+minute);
PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(context, idRequest,
intent, PendingIntent.FLAG_NO_CREATE);
// Cancel alarms
try {
alarmManager.cancel(pendingUpdateIntent);
Log.d(TAG, "Cancle-PeddingCodeGet: " + i);
} catch (Exception e) {
Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}
Log.d("cancle Alarm", hour + ":" + minute +" "+ calendar.get(Calendar.AM_PM) + calendar.get(Calendar.DAY_OF_MONTH) + "/" +
(calendar.get(Calendar.MONTH) + 1)+" idPedding: "+idRequest);
}
但是,当我删除所有警报时,我调试看到PendingIntent总是null。虽然 ,在我设置列表警报之前。 我无法检查它是否已经完成。 那么,如何删除警报列表?