1

我使用此代码从一个班级设置警报

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);

还是有更好/正确的方法来取消挂起的警报?

4

1 回答 1

2

只要与PendingIntent您用于注册警报的相同,您从哪里拨打电话都没有关系cancel()。如果底层意图和请求代码是等价的,则两个PendingIntents 是等价的(equals()returns )。true

于 2011-09-28T08:33:43.753 回答