如何在特定时间取消PendingIntent?

编程入门 行业动态 更新时间:2024-10-25 17:21:53
本文介绍了如何在特定时间取消PendingIntent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我没有声明取消时,AlarmManager可以完美地工作,但是当我声明取消时不触发。 b $ b这是代码:

AlarmManager works perfectly when I have not declared cancel but do not fire when I declare the cancel.. Here is the code:

Calendar c= Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 37); c.set(Calendar.SECOND, 0); Toast.makeText(this, c.getTime().toString(), Toast.LENGTH_LONG).show(); intent = new Intent(TestAlarm.this, TestAlarmService.class); pi = PendingIntent.getService(TestAlarm.this, 1, intent, 0); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);

和取消代码:

c.add(Calendar.HOUR_OF_DAY, 0); c.add(Calendar.MINUTE, 38); c.add(Calendar.SECOND, 0); PendingIntent pi1=PendingIntent.getService(TestAlarm.this, 1, intent, 0); AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE); //stopService(intent); am1.cancel(pi);

现在在这里我要启动我的 AlarmManager 一两分钟后在12:37取消并

Now here I want to start my AlarmManager to go off at 12:37 and cancel after one or two minutes...But Whenever I use cancel code the AlarmManager never fires... Thanks in Advance! :)

推荐答案

更改行 am1.cancel(pi); 到 am1.cancel(pi1); -您要取消原始的 PendingIntent

Change the line am1.cancel(pi); to am1.cancel(pi1); - you're cancelling the original PendingIntent

此外,在您的取消代码中,您正在调用 c.add()(添加到当前日期/时间),而不是 c.set()(明确设置下一个日期/时间)。通过调用 c.add(Calendar.MINUTE,38),实际上是在当前日历中增加38分钟,而不是将时间设置为12:38;

Also, in your cancel code, you're calling c.add() (which add on to the current date/time) instead of c.set() (which explicitly sets the next date/time). By calling c.add(Calendar.MINUTE, 38), you're actually adding 38 minutes to the current Calendar, instead of setting the time to 12:38;

更多推荐

如何在特定时间取消PendingIntent?

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

发布评论

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

>www.elefans.com

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