2013-03-24 114 views
0

我已經做了一個鬧鐘管理器來安排一些用戶定義的事件,它正在成功工作 然後我做了一個設置屏幕(從偏好活動),讓用戶改變一些設置像(前幾天報警)關於鬧鐘管理器在android

我的問題是,如果我3天的日程活動的開始日期之前的事件,那麼用戶之前通過設置到僅一天

然後我重新安排這些事件的事件開始日期前更改日子1天,是否意味着用戶將被通知兩次

  • 一個前3天
  • 一個前1天

,如果這是真的那麼我怎樣才能防止這種提前

回答

2

發生

由於每個報警伴隨的PendingIntent其具有唯一的哈希標識符。當使用相同的標識,即報警將覆蓋前者如文檔中表示: http://developer.android.com/reference/android/app/AlarmManager.html#set(int,長,android.app.PendingIntent)

附表報警。注意:...如果已經爲同一個IntentSender安排了警報,它將首先被取消...

請注意,PendingIntent由其參數和標識符表徵。

Intent intent = new Intent(mContext, YourTarget.class); 
// The hashcode works as an identifier here. By using the same, the alarm will be overwrriten 
PendingIntent sender = PendingIntent.getBroadcast(mContext, hashCode, intent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 
    mAlarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 
} else { 
    mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender); 
} 
1
public void cancel (PendingIntent operation) 

Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent 

匹配這一個(如由filterEquals(意向)中所定義), 將被取消。

所以,你可以撥打cancel在等待付款的意圖alarmManager.cancel(myPendingIntent) 和創建一個新的一個新的時間。

但是您不必顯式調用cancel,因爲只要filterEquals在將新的PendingIntent與前一個PendingIntent進行比較時返回true,那麼您的鬧鐘將僅在前一天開始。

公共布爾filterEquals(意向其他)

Determine if two intents are the same for the purposes of intent resolution 

(濾波)。也就是說,如果他們的行爲,數據,類型,類別, 和類別是相同的。這不會比較意圖中包含的任何額外數據 。