Frage

I am setting alarms at diffrent time . I want to delete a particulat alarm. Ex. I set 100 alarms at diiferents times , now I want to Delete an alarm set at 25 Feb 2012 10.45 AM. How Can I do that.

I written following code to set alarm.

       final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
       GregorianCalendar  gc=new GregorianCalendar();
       gc.set(2012, 1, 22, 10, 42,0);

       Intent intent = new Intent(this, AlarmService.class);
       gc.set(Calendar.AM_PM,0);

       final PendingIntent sender = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

       am.set(AlarmManager.RTC_WAKEUP, gc.getTimeInMillis(), sender);

I have a broadcast receiver to recieve the alarm.

War es hilfreich?

Lösung

You need to save the request code of alarm,that is 1 in your case here. The 2nd parameter you pass in your PendingIntent.getBroadcast() method is your request code for a particular alarm.So when you save request code along with the time for what you set alarm,it would be easy for you to get the particular alarm instance and then you can cancel it using:

AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, OneShotAlarm.class);    //OneShotAlarm is the broadcast receiver you use for alarm      
PendingIntent sender = PendingIntent.getBroadcast(context,alarm_request_code, intent, 0);
am.cancel(sender);

You should take care that each of your request code for different alarms should be unique,otherwise your alarm will be rescheduled with new time you set with the older request code.

Hope you get the point.

Andere Tipps

You might need to store it in some form of data structure to keep track of the time and the object. That being said, I have not tried storing AlarmManager objects before.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top