1

i have build a simple Android App. A Thread is collecting data, sleep 500ms, collecting and so on. The app should do this endless (until I stop it).

Now the thread run in a foreground service (in the activity and in a background service android kill the thread/service after a couple minutes).

If some data collected, the thread save them in a text file. (It collect 120 values in 1 minute, forms the mean and save this with timestamp).

When I am looking in the textfile, there data for 1-2 hours, every minute - like i want it. But then absence data for 10 minutes, then for 30 minutes are data for every minute, then absence data for 2 hours and so on.

I can not figure out: - if android sometimes not save the data, - or is it to stupid to continuously sleep every 500ms (sometimes sleep 2 hours?), - or android paused the hole service (randomly) - ...

Is there a better way to collect data periodically?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Richi
  • 23
  • 5
  • Can you tell us more about how the data is collected ? Are you sure you need it to run it that often ? Did you consider scheduling periodic jobs ? – Ariles Jan 29 '19 at 10:26
  • Actually it generates simply random 0/1 . – Richi Jan 29 '19 at 10:48
  • Later it should collect and save sensor data - at least every 10 seconds (as example magnetic field value) – Richi Jan 29 '19 at 10:56
  • Please check leonardkraemer's answer [here](https://stackoverflow.com/questions/49637967/minimal-android-foreground-service-killed-on-high-end-phone), he explains everything. – Ariles Jan 29 '19 at 11:22
  • Also see [this](https://stackoverflow.com/a/54354069/6759241) – greeble31 Jan 29 '19 at 15:23

1 Answers1

0

Android goes in the idle modus.

I solve the problem with a Partial Wakelock.

Richi
  • 23
  • 5