android - AlarmManager not creating an alarm -
i'm attempting add "reminders" functionality app. want set alarmmanager
, fire notification
when alarm happens.
i've added few methods create alarm, cancel alarm, , see if alarm active.
the following code in settings.class
extends activity
:
private boolean getremindersenabled() { return (pendingintent.getbroadcast(this, 1, new intent(this, main.class), pendingintent.flag_no_create) != null); } private void enablereminders() { alarmmanager alarmmgr; pendingintent alarmintent; alarmmgr = (alarmmanager)getsystemservice(context.alarm_service); intent intent = new intent(this, main.class); alarmintent = pendingintent.getbroadcast(this, 0, intent, 0); calendar calendar = calendar.getinstance(); calendar.settimeinmillis(system.currenttimemillis()); calendar.set(calendar.hour_of_day, mdaystart); calendar.set(calendar.minute, 0); alarmmgr.setrepeating(alarmmanager.rtc_wakeup, calendar.gettimeinmillis(), sixy_minutes, alarmintent); mremindersenabled = getremindersenabled(); populatereminders(); } private void disablereminders() { alarmmanager service = (alarmmanager)getsystemservice(context.alarm_service); intent intent = new intent(this, main.class); pendingintent pending = pendingintent.getbroadcast(this, 0, intent, pendingintent.flag_cancel_current); service.cancel(pending); mremindersenabled = getremindersenabled(); populatereminders(); }
when call enablereminders()
, creates alarm. however, getremindersenabled()
returns false. there i'm failing do?
getremindersenabled()
should use same requestcode
pendingintent
creates alarm. in case, using 1
, correct 0
.
private boolean getremindersenabled() { return (pendingintent.getbroadcast(this, 0, new intent(this, main.class), pendingintent.flag_no_create) != null); }
[edit]
to cancel alarm, before calling service.cancel(intent);
, call pending.cancel();
using flag flag_update_current
pendingintent pending = pendingintent.getbroadcast(this, 0, intent, pendingintent. flag_update_current); pending.cancel(); service.cancel(pending);
Comments
Post a Comment