重复闹钟不取消

发布于 2024-11-18 13:23:20 字数 1179 浏览 3 评论 0原文

我设置了重复警报,我的问题是取消后它不会取消(我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泼猴你往哪里跑 2024-11-25 13:23:20

尝试使用相同的上下文来表达意图。
意图必须匹配,我认为您的问题是您在尝试取消警报时使用了不同的上下文。

alarmIntent = PendingIntent.getBroadcast(this, 0, intentToCancel, 0);

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.

alarmIntent = PendingIntent.getBroadcast(this, 0, intentToCancel, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文