0

如何在已启动的预定时间取消一个 AlarmManager。我正在像这样启动一个 AlarmManager。

我想在 5 小时后取消它。我怎样才能做到这一点?

AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyStartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);
4

3 回答 3

1

从现在开始再设置一个 5 小时的闹钟。当此警报警报响起时,取消第一个警报。

AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intentForCancelling = new Intent(context, ReceiverForCancelling.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, intentForCancelling,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.roll(Calendar.HOUR_OF_DAY, 5);
service.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), pending)

并且,ReceiverForCancelling您可以取消第一个警报。
编辑::

AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReceiverForCancelling.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
service.cancel(pending);
于 2012-07-31T05:54:03.777 回答
0

你可以试试下面的代码

Intent intentstop = new Intent(this, RefreshFeedsReceiver.class);
PendingIntent senderstop = PendingIntent.getBroadcast(this,
                            0, intentstop, 0);
AlarmManager alarmManagerstop = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManagerstop.cancel(senderstop);
于 2012-07-31T05:42:00.170 回答
0

检查以下代码以设置和取消警报

 public void SetAlarm(Context context)
 {
     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
     Intent i = new Intent(context, Alarm.class);
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
     am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
 }

 public void CancelAlarm(Context context)
 {
     Intent intent = new Intent(context, Alarm.class);
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
     alarmManager.cancel(sender);
 }
于 2012-07-31T05:55:11.377 回答