2012-02-15 173 views
2

我正在開發一個警報應用程序,因爲我使用的警報管理器概念,該用戶可以選擇不。 (例如,他選擇5天后警報將在5天之前)比較天數列表來自sqlite數據庫。我寫了一些代碼其工作問題是,當用戶更改警報剩餘天數(例如, 3而不是5)設置之前(5天)警報不清楚總共有8個警報出現(5 + 3)請任何人解決我的問題。如何清除報警管理器中的先前報警?

dbcal.set(currentcal.get(Calendar.YEAR), mnth,dt); 
Log.e("dbCal Alarm","first"+dbcal.getTime()); 
if(!dbcal.getTime().before(currentcal.getTime())){ 
    uniqueno++; 
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(SampledateActivity.this,AlarmReceiver.class); 
    PendingIntent pi = PendingIntent.getBroadcast(SampledateActivity.this, uniqueno, intent, 0); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, dbcal.getTimeInMillis(), pi); 
} 
for(int k = 1 ; k <= time ;k++){ 
    Log.e("Entered to alarm","yes"); 
    dbcal.add(Calendar.HOUR, -24); 
    if(!dbcal.getTime().before(currentcal.getTime())){ 
     uniqueno++; 
     Intent intentlocal = new Intent(SampledateActivity.this,AlarmReceiver.class); 
     PendingIntent pilocal = PendingIntent.getBroadcast(SampledateActivity.this, uniqueno, intentlocal, 0); 
     alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, dbcal.getTimeInMillis(), pilocal); 
     Log.e("dbCal Alarm","k: "+"k value:"+k+(time*i+k)+":"+dbcal.getTime()); 
    } 
} 
+0

下面的URL可能對您有所幫助 http://stackoverflow.com/questions/7933669/two-buttons-with-pendingintents-widget – 2012-02-15 12:17:17

回答

8

創建PendingIntent與您用於創建PendingInent啓動Alarm相同requestCode

Intent intentlocal = new Intent(SampledateActivity.this,AlarmReceiver.class); 
PendingIntent pilocal = PendingIntent.getBroadcast(SampledateActivity.this, 
                 uniqueno, intentlocal, 0); 
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.cancel(pilocal); 
pilocal.cancel(); 
+0

感謝您的答覆我使用我們的代碼,但它不工作 – NareshRavva 2012-02-15 11:04:31

+2

我有兩次檢查上面的代碼,但它不工作 – NareshRavva 2012-02-15 11:47:59

+0

當我選擇前5天,我已經改變日期和檢查一天之間的鬧鐘,然後我選擇1天,我設置日期爲我的要求之前設置鬧鐘也來了,即我得到了5個鬧鐘,我認爲這些都是未決的鬧鐘之前,我選擇的情況下,即5天 – NareshRavva 2012-02-15 11:55:23

0

我得到了解決我的問題。每次我刷新我的活動。即,當我點擊設置的鬧鐘按鈕,我已經刷新了我的活動。因此在設置警報之前將刪除並創建新輸入的警報。