我尝试使用AlarmManager.cancel()取消闹钟,但显然它无法正常工作。我已经读过某个地方,我必须使用PendingIntent.FLAG_UPDATE_CURRENT,如代码所示,是我使用的。在设置闹钟时,我希望程序使用代码自动设置闹钟,因此代码如下。
setAlarm功能:
public void setAlarm(DateTime date, String message) {
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_MESSAGE, message);
i.putExtra(AlarmClock.EXTRA_HOUR, date.getHourOfDay());
i.putExtra(AlarmClock.EXTRA_MINUTES, date.getMinuteOfHour());
i.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
PendingIntent pendingIntent = PendingIntent.getActivity(appContext.getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) appContext.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
return;
}
cancelAlarm功能:
public void cancelAlarm(DateTime date, String message) {
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_MESSAGE, message);
i.putExtra(AlarmClock.EXTRA_HOUR, date.getHourOfDay());
i.putExtra(AlarmClock.EXTRA_MINUTES, date.getMinuteOfHour());
i.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
PendingIntent pendingIntent = PendingIntent.getActivity(appContext.getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) appContext.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
return;
}
有什么想法吗?我的智慧结束了。提前谢谢!
答案 0 :(得分:1)
设置闹钟时,请执行以下操作:
manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);
这会将警报设置为System.currentTimeMillis()
,这将使警报立即关闭。您取消闹钟的呼叫不起作用,因为当您拨打取消呼叫时,闹钟已经消失,不再存在。