如何取消AlarmManager [英] How to cancel an AlarmManager
本文介绍了如何取消AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经设置了如下alarmManager:
I have setup an alarmManager as follows:
Intent intent = new Intent(TopActivity.this, RecordActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(Utils.KEY_RECORD_TIME, recordLength);
intent.putExtra(Utils.KEY_REC_START_TIME, start);
saveTimeAndLength(start, recordLength);
PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);
以下内容应该取消它,但它总是失败.我想念什么?
The following is supposed to cancel it, but it always fail. What am I missing?
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(TopActivity.this, RecordActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(Utils.KEY_RECORD_TIME, start);
intent.putExtra(Utils.KEY_REC_START_TIME, recordLength);
PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, 0);
try {
alarmManager.cancel(pintent);
Log.e(TAG, "Cancelling all pending intents");
} catch (Exception e) {
Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}
我已经在StackOverflow上阅读了很多答案,但仍然无法找出问题所在.
预先感谢!
I have read lots of answers on StackOverflow but still could not figure out what's the problem.
Thanks in advance!
推荐答案
我将在此处发布答案,希望这可以帮助其他人节省一些时间.
在两侧添加标志PendingIntent.FLAG_UPDATE_CURRENT
可以解决此问题.
I will post my answer here hoping that would help others saving some time.
Somehow adding the flag PendingIntent.FLAG_UPDATE_CURRENT
in both sides solve the problem.
PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
解决了问题.
设置闹钟
solved the problem.
Setting Alarm
Intent intent = new Intent(TopActivity.this, RecordActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(Utils.KEY_RECORD_TIME, recordLength);
intent.putExtra(Utils.KEY_REC_START_TIME, start);
saveTimeAndLength(start, recordLength);
PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent);
取消警报
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(TopActivity.this, RecordActivity.class);
PendingIntent pintent = PendingIntent.getActivity(TopActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
alarmManager.cancel(pintent);
Log.e(TAG, "Cancelling all pending intents");
} catch (Exception e) {
Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}
这篇关于如何取消AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文