我为预约提醒创建了一个警报管理器,并且工作正常。但是当我想要取消闹钟时,它仍会按时发射。警报取消无效。以下是设置和取消警报的代码。感谢
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.APPOINTMENT_REMINDER_NOTIFICATION");
notificationIntent.putExtra("APPOINTMENT_TIME", date);
notificationIntent.putExtra("APPOINTMENT_ID", appointment_id);
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, time_remaining);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
Intent intent = new Intent("android.media.action.APPOINTMENT_REMINDER_NOTIFICATION");
//PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT).cancel();
//Intent intent = new Intent(context, AlarmReceiver.class);
//alarmManager.cancel(PendingIntent.getService(context, 100, intent, 0));
PendingIntent pIntent = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pIntent);
pIntent.cancel();
答案 0 :(得分:5)
使用alammanager
时要记住的第一件事是 -
您必须在创建和取消警报时使用相同的context
。
这包括他们的行动,数据,类型,类和类别是相同的。这个 不会比较意图中包含的任何额外数据。
在这种情况下,您可以使用getApplicationContext()
来简化操作。所以下面的代码应该有效 -
设置闹钟 -
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent("android.media.action.APPOINTMENT_REMINDER_NOTIFICATION");
notificationIntent.putExtra("APPOINTMENT_TIME", date);
notificationIntent.putExtra("APPOINTMENT_ID", appointment_id);
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(getApplicationContext(), 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, time_remaining);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
取消闹钟 -
PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pIntent);
答案 1 :(得分:0)
使用操作名称创建新的Intent。 不要通过onRecive方法获得意图
对我来说有效
答案 2 :(得分:0)
您需要提供完全相同的意图
private void resetIntentWithAlarm(int time){
Intent intentAlarm = new Intent(getApplicationContext(), DownloadService.class);
intentAlarm.putExtra(Your Key, Your stuff to pass here);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(
getApplicationContext(),
YOUR_ID,
intentAlarm,
PendingIntent.FLAG_UPDATE_CURRENT
);
if (time != 0) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (60L * 1000L * time), (60L * 1000L * time), pendingIntent);
Log.i(TAG, "Alarm setted on for " + time + " mins.");
}
// if TIME == Zero, cancel alaram
else {
alarmManager.cancel(pendingIntent);
Log.i(TAG, "Alarm CANCELED. Time = " + time);
}