2010-05-30 224 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); 

這可能不適用於你的情況,但如果你還沒有嘗試過這種方法,它可能是一個很好的開始!