2

i am developing one alert app in that i am using alarm manager concept in that user can select no. of days before to came alert(ex.if he select 5 days the alert will come before 5 days) comparing days list is coming from sqlite data base.i write some code its working problem is that when user changes alarm remainder days(ex.3 instead of 5) before setting(5 days)alarms are not clear total 8 alarms are came(5+3) please anyone resolve my problem.

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());
  }
}
Gowtham
  • 11,853
  • 12
  • 43
  • 64
NareshRavva
  • 823
  • 3
  • 21
  • 50
  • The following url may helpful to you http://stackoverflow.com/questions/7933669/two-buttons-with-pendingintents-widget – prasad.gai Feb 15 '12 at 12:17

2 Answers2

8

Create the PendingIntent with same requestCode that you used to create the PendingInent to start the Alarm.

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();
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • thanks for your response i am using ur code but its not working – NareshRavva Feb 15 '12 at 11:04
  • when i select first 5 days and i have change the date and check one alarm in between days and next i select 1 day and i set the date as my requirement the before setting alarms also came i.e i got 5 alarms i think those are pending alarms before i selected case i.e 5 days – NareshRavva Feb 15 '12 at 11:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7782/discussion-between-user1084935-and-lalit-poptani) – NareshRavva Feb 16 '12 at 09:24
0

I got the solution to my problem. Every time i have refresh my activity. i.e.., when i click on the set alarm button in that i have refresh my activity. so before setting alarms are deleted and newly entered alarms are created.

NareshRavva
  • 823
  • 3
  • 21
  • 50