android - Why isn't my AlarmManager working? -
i trying make alarm triggers event number of seconds now, 1 time, within dialogfragment.
here relevant code, put in oncreate():
broadcastreceiver = new broadcastreceiver() { @override public void onreceive(context c, intent i) { toast.maketext(c, "rise , shine!", toast.length_long).show(); } }; getactivity().registerreceiver(broadcastreceiver, new intentfilter(alarm_manager_tag) ); pendingintent = pendingintent.getbroadcast(getactivity(), 0, new intent(alarm_manager_tag), 0 ); alarmmanager = (alarmmanager)(getactivity().getsystemservice( context.alarm_service ));
and when press start button:
alarmmanager.set(alarmmanager.elapsed_realtime_wakeup, timestampend, pendingintent);
in case timestampend = system.currenttimemillis() + 10 * 1000;
, 10 seconds.
and have overridden destroy:
@override public void ondestroy() { alarmmanager.cancel(pendingintent); getactivity().unregisterreceiver(broadcastreceiver); super.ondestroy(); }
and yet, nothing happens reason.
you're passing alarmmanager.elapsed_realtime_wakeup
, means alarmmanager
going use systemclock.elapsedrealtime()
when checking timestamps. since you're passing calculated off of system.currenttimemillis()
, going have wait 47 years before alarm fires.
either change first argument alarmmanager.rtc_wakeup
or change timestamp calculated off of systemclock.elapsedrealtime()
.
Comments
Post a Comment