取消发起它的其他类的警报

时间:2011-09-28 08:04:09

标签: android android-alarms

我使用此代码从一个班级设置闹钟

Intent myIntent = new Intent(ClassOne.this, AlarmService.class);
pendingIntent = PendingIntent.getService(ClassOne.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentDate.getTime());
long when = calendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, (7 * 24 * 60 * 60) * 1000, pendingIntent);

现在我需要从其他类中取消待处理的警报。我可以使用这段代码吗?

Intent myIntent = new Intent(ClassTwo.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(ClassTwo.this, 0, myIntent, 0);
AlarmManager alarmManagerCancel = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManagerCancel.cancel(pendingIntent);

或者有更好/正确的方法来取消待处理的警报?

1 个答案:

答案 0 :(得分:2)

只要PendingIntent与用于注册闹钟的{0}}相同,那么从哪里调用cancel()并不重要。如果底层意图和请求代码是等价的,则两个PendingIntent是等价的(equals()返回true)。