当我没有声明取消时,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?
发布评论