android - alarm manager fails notifying -


i have simple code setup notify users @ specific time interval. although getdate method returns future time, still triggers right away!

intent notificationintent = new intent(act, notificationpublisher.class); notificationintent.putextra(notificationpublisher.notification_id, idtt); notificationintent.putextra(notificationpublisher.notification, notification); pendingintent pendingintent = pendingintent.getbroadcast(act, idtt, notificationintent, pendingintent.flag_update_current); alarmmanager alarmmanager = (alarmmanager) act.getsystemservice(context.alarm_service);  date dd = getdate(datett);  calendar calendar = calendar.getinstance(); calendar.settimeinmillis(system.currenttimemillis()); calendar.set(calendar.month, dd.getmonth()); calendar.set(calendar.year, dd.getyear()); calendar.set(calendar.day_of_month, dd.getday()); calendar.set(calendar.hour_of_day, dd.gethours()); calendar.set(calendar.minute, dd.getminutes());  alarmmanager.set(alarmmanager.elapsed_realtime_wakeup,calendar.gettimeinmillis(), pendingintent); 

fixed date calendar object - since date.getmonth() , other methods deprecated, had create calendar object, , year

create calendar object date -

calendar caltemp = calendar.getinstance(); caltemp.settime(ddtt); 

get month, year , other fields -

calendar.set(calendar.month, caltemp.get(calendar.month)); calendar.set(calendar.year, caltemp.get(calendar.year)); calendar.set(calendar.date, caltemp.get(calendar.date)); calendar.set(calendar.hour_of_day, caltemp.get(calendar.hour_of_day)); calendar.set(calendar.minute, caltemp.get(calendar.minute)); 

getdate method jjust converts date system timezone

        private date getdate(string datestring) {             simpledateformat formatter = new simpledateformat("mm/dd/yyyy hh:mm z");             //formatter.settimezone(timezone.gettimezone("edt"));             date value = null;             try {                 value = formatter.parse(datestring);             } catch (parseexception e) {                 e.printstacktrace();             }             simpledateformat dateformatter = new simpledateformat("mm/dd/yyyy hh:mm");             dateformatter.settimezone(timezone.getdefault());             string dt = dateformatter.format(value);              try {                 value = dateformatter.parse(dt);             } catch (parseexception e) {                 e.printstacktrace();             }              return value;         } 

i'm missing obvious here, don't know is!

you should use rtc_wakeup instead of elapsed_realtime_wakeup.

example:

int trigger_time= system.currenttimemillis() + 30 * 1000; //thirtysecondsfromnow    if(build.version.sdk_int >= 19) {     am.setexact(alarmmanager.rtc_wakeup, trigger_time,              pendingintent); } else {     am.set(alarmmanager.rtc_wakeup, trigger_time,              pendingintent); } 

explanation:

1) elapsed_realtime fires pending intent based on amount of time since device booted, doesn't wake device. elapsed time includes time during device asleep.

2) elapsed_realtime_wakeup wakes device , fires pending intent after specified length of time has elapsed since device boot.

3) rtc fires pending intent @ specified time not wake device.

4) rtc_wakeup wakes device fire pending intent @ specified time.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -