如何取消AlarmManager

编程入门 行业动态 更新时间:2024-10-11 07:33:49
本文介绍了如何取消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

本文发布于:2023-11-27 13:19:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!