android - What is the proper way for implementing an alarm that runs once, but stays alive after reboot? -


in app, have alarm set when run, launches service perform task (in app, sending text message without user interaction). message can set 2 hours in future, or year in future. here how implement it.

in main activity

user types message, choses date , time (translated milliseconds), , alarm time (the milliseconds), saved persistently. call service, have set sticky, , boot on startup.

in service.

when service gets called (which after first time schedule message, running, time), creates alarm saved alarm time. good, because if phone reboots, alarm recreated again.

here problem

after alarm time has passed, , message sent, don't see way shut down service running. service gets killed , restarted (like when phone reboots), launches, sees alarm time in past, , launches service sends message. until clear data on app, random duplicate messages sent out, @ random future times.

my solution (not elegant).

after message sends first time, add 50 years (in milliseconds), alarm time, , resave persistently. way won't trigger again until user sets time themselves. seems have worked, imo terrible solution.

there has more proper way of setting persistent alarm run once. can has more experience alarmmanager advise me?

you can register broadcastreciever onbooted start alarm again - definitly need save alarm-time in sharedpreferences or sqlite database...

went through same sooner


Comments