alarmmanager - android prevent immediate trigger of alarm service if alarm time has passed for the day -


the reference alarm manager says

if stated trigger time in past, alarm triggered immediately.

i facing problem in application. here alarm manager code :

intent myintent = new intent(getactivity(), dinneralarmreceiver.class);                 pendingdinnerintent = pendingintent.getbroadcast(getactivity(), 0, myintent, 0);                  calendar calendar = calendar.getinstance();                 calendar.set(calendar.hour_of_day, hourofday);                 calendar.set(calendar.minute, minute);                  alarmmanager.set(alarmmanager.rtc_wakeup,                         calendar.gettimeinmillis(), pendingdinnerintent); 

is there workaround problem?

-----edit------

i have written code estimate if set time alarm before current time . here above portion corresponding changes :

calendar calendar = calendar.getinstance();                 long currenttime = calendar.gettimeinmillis();                 calendar.set(calendar.hour_of_day, hourofday);                 calendar.set(calendar.minute, minute);                 long settime = calendar.gettimeinmillis();                 timestamp settimestamp = new timestamp(settime);                 timestamp currenttimestamp = new timestamp(currenttime);                 if (settimestamp.after(currenttimestamp))                 {                     alarmmanager.set(alarmmanager.rtc_wakeup,                             calendar.gettimeinmillis(), pendingdinnerintent);                 }                 else                 {                 } 

what should alarmmanager in case settimestamp before currenttimestamp ?

you don't need create timestamps. can calendar.

calendar calendar = calendar.getinstance(); calendar.set(calendar.hour_of_day, hourofday); calendar.set(calendar.minute, minute);  if(calendar.before(calendar.getinstance())) {     calendar.add(calendar.date, 1); }  alarmmanager.set(alarmmanager.rtc_wakeup,     calendar.gettimeinmillis(), pendingdinnerintent); 

i mention of kitkat, if targetsdkversion 19 or above, alarmmanager#set() method not exact. if want alarm fire @ exact time, you'll need use setexact*() method.


Comments