1

I have a question on handlers and their existence when device is rebooted, Say i have executed 3 handlers with a delay of 3 hrs using postDelayed(), i just wanted to know if, for an instance the device was rebooted, do those threads still exist? or do i need to execute them again?. if so, how do i automatically execute handlers after device reboot?. I've read some threads that i need to start a receiver in turn to run a service but im really confused on how to go about this.

lemoncodes
  • 2,371
  • 11
  • 40
  • 66

2 Answers2

2

i just wanted to know if, for an instance the device was rebooted, do those threads still exist?

No. They also will not exist for your three-hour duration, anyway, as Android is likely to have terminated your process first. Please use AlarmManager for periodic work like this.

if so, how do i automatically execute handlers after device reboot?

You don't. You can use a BOOT_COMPLETED BroadcastReceiver to reschedule alarms in AlarmManager, though.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • hmmm yes, my app is more like an alarm thing, but i wanted to schedule an alarm, say 3 hours from now, and i can't think of anything else but to use handlers to fire the alarm at exactly the right time. Do you know how to fire up the alarm without using the handlers? i mean firing the alarm accurately, like the user will choose / set the time when to fire the alarm? – lemoncodes Sep 19 '12 at 11:46
  • 1
    "Do you know how to fire up the alarm without using the handlers?" -- use `AlarmManager`, as I wrote. "i mean firing the alarm accurately, like the user will choose / set the time when to fire the alarm?" -- use `AlarmManager`, as I wrote. – CommonsWare Sep 19 '12 at 11:52
  • i've done scheduling an alarm and also tried stopping it, but how do i auto stop an alarm say after 3 repeats?.. – lemoncodes Sep 19 '12 at 13:36
  • 1
    @lemoncodes: Call `cancel()` on `AlarmManager` after your third repeat, to cancel an existing scheduled event. – CommonsWare Sep 19 '12 at 15:49
  • the problem is how do i flag the repeats, how do i know or count the repeeats, setReapting() returns void, where do i count them? – lemoncodes Sep 21 '12 at 00:55
  • 1
    @lemoncodes: In a file. Or a `SharedPreferences`. Or a database. – CommonsWare Sep 21 '12 at 10:33
0

The handlers are gone after reboot. If you want to have them set on Android boot, you can write a system app that always launches on Android start.

Community
  • 1
  • 1
m0skit0
  • 25,268
  • 11
  • 79
  • 127