我正在制作一个事件应用程序,用户可以为他想要的事件设置提醒。所以我使用alarmManager 来创建警报。我想在我的主要活动中添加一个取消所有选项,以便我可以取消我的应用程序创建的所有警报。以相同意图取消警报的常用方法并没有真正帮助,因为我将警报设置在与我想要取消它们的活动不同的活动上。那么有没有办法取消我的应用程序创建的所有警报?谢谢!
问问题
3426 次
1 回答
2
执行取消的代码不必是警报的发起者。您的代码通过识别创建它的 PendingIntent 来识别要取消的警报。PendingIntent
您可以按如下方式“制造”原件的传真件。. .
String pname = this.getPackageName();
// manufacture an appropriate context
Context mycontext = null;
try {
mycontext = createPackageContext(pname,CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
// handle exception here
e.printStackTrace();
}
// and generate a pendingintent
PendingIntent pi = PendingIntent.getService(mycontext,
0, new Intent(mycontext, myalarmreceiver.class), 0);
// Now use alarmmanager to terminate the alarm
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(pi);
这可能不适用于您的情况,但如果您还没有尝试过这种方法,这可能是一个不错的起点!
于 2010-06-29T03:11:14.223 回答