0

I am implementing simple timer and I require it to perfectly measure time.
Problem is that even using foreground, START_STICKY and wake lock when timer is started somehow time measuring is killed after some time when phone is not used. I assume it is going in deep sleep mode and then somehow my partial wakelock (only cpu required) is ignored. Is there some way to prevent that? Just for the record, I am going with that solution as I think that simple time counting is not a big deal for cpu and it won't drain battery as hell.

One more thing, when testing on API 19 it seems not likely to be killed. On API 25, device with 'cleaner' Android it is usually killed after 30min to 2h after running. Kill is in my opinion not related to too high memory usage as my app is the only one running in background ( user started).

Thanks in advance for any ideas.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
kolboc
  • 805
  • 1
  • 8
  • 22
  • 1
    https://developer.android.com/training/monitoring-device-state/doze-standby.html – CommonsWare Oct 28 '17 at 17:32
  • 2
    Beyond that, you do not need to keep the device running to keep track of time, just as humans do not need to keep staring at a clock to keep track of time. "I think that simple time counting is not a big deal for cpu and it won't drain battery as hell" -- you are very mistaken. Keeping the CPU on all the time is a significant battery drain. This is why Google has to introduce things like Doze mode (see link in previous comment), to prevent developers from keeping the CPU on for no good reason. – CommonsWare Oct 28 '17 at 17:35
  • oh man i didn't know that. Big thank you !! Jumping into reading ;) So i suppose I would have to track it another way when device is sleeping and live updating UI only when device is 'active'. – kolboc Oct 28 '17 at 17:35
  • One more question @CommonsWare , the proper way of detecting that I don't have to update UI for the user would be screen off right? – kolboc Oct 28 '17 at 17:41
  • No, use `onStop()` of your activity. That is the signal that your activity is no longer visible. When your activity becomes visible again, and you are called in `onStart()`, start showing the elapsed time again, based on the original start time. You will need to save that original start time in a database, `SharedPreferences`, or other form of file, in case your process gets terminated while your UI is not in the foreground. – CommonsWare Oct 28 '17 at 17:44
  • "I would have to track it another way when device is sleeping" -- that's what saving the start time in a file is for. Again, think of what you do as a human. You look at a clock and make a note of the time. You then go off and do something else. Later, you look at the clock again, see the current time, and calculate the elapsed time from when you looked at the clock earlier. You did not have to keep staring at the clock. Saving the start time in a file is the "make note of the time" step. – CommonsWare Oct 28 '17 at 17:46
  • Thanks, you are right. I have tendency to make work arounds when started with some idea and sometimes its getting into quite bad approach to solve the problem. – kolboc Oct 28 '17 at 21:51

0 Answers0