2010-05-30 225 views
2

我正在制作活动应用程序,用户可以为他想要的活动设置提醒。 所以我使用alarmManager来创建警报。我想将取消所有选项放到我的主要活动中,以便我可以取消由我的应用程序创建的所有警报。 用相同的意图取消闹钟的常用方法并不能真正帮助我将闹钟设置为与我想要取消闹钟的活动不同。 那么有没有办法取消我的应用程序创建的所有闹钟? 谢谢!Android取消所有闹钟设置

回答

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

这可能不适用于你的情况,但如果你还没有尝试过这种方法,它可能是一个很好的开始!