Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the proper way for implementing an alarm that runs once, but stays alive after reboot?
    primarykey
    data
    text
    <p>In my app, I have an alarm set that when run, launches a service that will perform a task (in my app, it is sending a text message without user interaction). This message can be set for 2 hours in the future, or a year in the future. Here is how I implement it.</p> <p><strong>In my main activity</strong></p> <p>user types message, choses a date and time (translated to milliseconds), and that alarm time (the milliseconds), is saved persistently. I then call a service, which i have set to be sticky, and boot on startup. </p> <p><strong>In my service.</strong></p> <p>When the service gets called (Which after the first time you schedule a message, it is now running, all the time), it creates the alarm with the saved alarm time. This is good, because if the phone reboots, the alarm will get recreated again.</p> <p><strong>Here is the problem</strong></p> <p>After the alarm time has passed, and the message is successfully sent, I don't see a way to shut down that service from always running. So as soon as the service gets killed and restarted (like when the phone reboots), it launches, sees the alarm time is in the past, and immediately launches the service that sends the message. So until I clear data on the app, I get random duplicate messages sent out, at random future times.</p> <p><strong>My solution (not elegant).</strong></p> <p>After the message sends the first time, i add 50 years (in milliseconds), to the alarm time, and resave it persistently. This way it won't trigger again until the user actually sets the time themselves. This seems to have worked, but IMO is a terrible solution. </p> <p><strong>There has to be a more proper way of setting a persistent alarm to only run once. Can someone who has more experience with AlarmManager advise me?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload