0

I have an Android Application that uses a Countdown Timer that lasts for around 2 days. What is the best method to avoid my Countdown Timer from being killed by the Android Application Manager even if the user enables a power saving mode or restarts their phone ? (Sorry if this is a senseless question to answer, for the reason that I am new to Android development.)

K.Milli
  • 81
  • 2
  • 10
  • Do you know what the options are? have you tried anything? – CallumDA Nov 26 '16 at 10:18
  • 1
    @CallumDA33, I have tried the foreground service but it got killed after the device restarted at first. – K.Milli Nov 26 '16 at 10:25
  • http://stackoverflow.com/questions/6391902/how-to-start-an-application-on-startup, note that if you want a foreground service you must have a notification. http://stackoverflow.com/questions/10962418/how-to-startforeground-without-showing-notification – T.S Nov 26 '16 at 11:27

1 Answers1

0

What is the best method to avoid my Countdown Timer from being killed by the Android Application Manager even if the user enables a power saving mode or restarts their phone ?

That is not possible. Moreover, it is very wasteful (tying up system RAM, spending CPU time). If you want to get control at a certain time in the future, use methods on AlarmManager (e.g., setAlarmClock()). If you want to find out how much time remains between now and that certain time in the future, find out the current time (e.g., System.currentTimeMillis()) and subtract that from the future time to calculate the difference in times. To handle a reboot, you would need to set up the AlarmManager again, by using a BroadcastReceiver set up to respond to ACTION_BOOT_COMPLETED.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you very much, would I need to create a database to save the current time and future time so it can be accessible after the reboot because I'm really bad at seeing up databases – K.Milli Nov 26 '16 at 14:48
  • @K.Milli: You would not need to save the current time, as the current time is always available from the system. You need to save the future time in persistent storage for any solution if you want that data to exist after a reboot. – CommonsWare Nov 26 '16 at 14:58
  • Sorry, hopefully this is last question— do you possibly have a resource of how I can incorporate the Alarm Manager and BroadcastReceiver to update my GUI? Because I need to display the days, hours, minutes and seconds – K.Milli Nov 26 '16 at 15:12
  • @K.Milli: While your activity is around, feel free to use something like `CountDownTimer` for that. The vast majority of the time, the user is not in your UI, and you will not have a process running. In those cases, if you want to get control when the future time arrives, *that* is what `AlarmManager` is for. – CommonsWare Nov 26 '16 at 15:17
  • I get it! Thank you so much! – K.Milli Nov 26 '16 at 15:19
  • #mostNicestPersonOnStackOverflow – K.Milli Nov 26 '16 at 15:20