重复闹钟不取消
我设置了重复警报,我的问题是取消后它不会取消(我用 Log.v() 检查这一点,
这就是我创建警报的方式(在 IntentService 中)
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intentToFire = new Intent(CAlarmReceiver.ACTION_CHECK_ALARM);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0);
private void rescheduleAlarm() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, TIME_TO_REFRESH);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), CHECK_TIME, alarmIntent);
}
然后在 Activity 中我有一个单击按钮后,它会调用此代码
private OnClickListener btnCloseApplicationListener = new OnClickListener() {
public void onClick(View v) {
intentToCancel = new Intent(CAlarmReceiver.ACTION_CHECK_ALARM);
alarmIntent = PendingIntent.getBroadcast(v.getContext(), 0, intentToCancel, 0);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.cancel(alarmIntent);
finish();
}
};
执行 finish() 后,我会不断看到 DDMS 窗口中的日志。 我怎样才能取消它? 提前致谢!
I have a repeating alarm set, my problem is that after cancelling it doesn't cancel (I'm checking this with Log.v()
This is how I create the alarm (in an IntentService)
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intentToFire = new Intent(CAlarmReceiver.ACTION_CHECK_ALARM);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0);
private void rescheduleAlarm() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, TIME_TO_REFRESH);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), CHECK_TIME, alarmIntent);
}
Then in an Activity I have a button on upon it's click it calls this code
private OnClickListener btnCloseApplicationListener = new OnClickListener() {
public void onClick(View v) {
intentToCancel = new Intent(CAlarmReceiver.ACTION_CHECK_ALARM);
alarmIntent = PendingIntent.getBroadcast(v.getContext(), 0, intentToCancel, 0);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.cancel(alarmIntent);
finish();
}
};
After the finish() is executed I keep seeing the logs I have in DDMS window.
How can I cancel it?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用相同的上下文来表达意图。
意图必须匹配,我认为您的问题是您在尝试取消警报时使用了不同的上下文。
try using same context for the intent.
The intents have to match and I think your problem is that you are using different context when trying to cancel the alarm.