0

I have an app with a service with return Service.START_STICKY in his onStartCommand. This service log the user locations to send it every hours in batch to the server. To economize the battery life I don't store the log to the disk, i simply keep it in memory. problem :

  1. when the user kill the application, then it's look like the service is also killed but the ondestroy of the service is not fired. So how can i know when the service is being to be close/destroyed to save to disk all the log to retrieve it later ?

  2. when the system restart the service thank to START_STICKY it's seam the onStartCommand is not called. is it a normal behavior ?

zeus
  • 12,173
  • 9
  • 63
  • 184
  • refer this : https://stackoverflow.com/questions/5862045/can-i-detect-when-my-service-is-killed-by-advanced-task-killer – Aman Grover Jul 13 '17 at 12:37
  • I think that you are wasting battery exactly by storing data in memory. Why don't you just call a AlarmManager every hour to do whatever you want? – Vladyslav Matviienko Jul 13 '17 at 12:41
  • @VladMatvienko: it's exactly what i do, i call an alarm manager every hours to send to the server the collected locations (network use lot of baterry so this why i send locations in batch every hours). but i store these location in memory (and it's not use any noticeable memory) – zeus Jul 13 '17 at 12:44
  • so you are tracking the location all the time? – Vladyslav Matviienko Jul 13 '17 at 12:45
  • "i store these location in memory" -- do not do that. Heap space is **only a cache**, because your process can be terminated at any point in time without warning. Save the data to disk. "So how can i know when the service is being to be close/destroyed to save to disk all the log to retrieve it later ?" -- that is not possible, by definition. If you are not being called with `onDestroy()`, your process is simply being terminated. – CommonsWare Jul 13 '17 at 12:46
  • @VladMatvienko: yes all the time – zeus Jul 13 '17 at 13:05
  • @CommonsWare : so how to efficiently save the lat/lng to the disk ? i mean it's look much more complicate, because if the process can be killed like this then maybe the file can be half written when the process is killed ? – zeus Jul 13 '17 at 13:07
  • @CommonsWare: so to resume i need to save to disk lat/long/accuracy/time but what the best way to do this ? i preferences maybe? later i send them to the server in json string – zeus Jul 13 '17 at 13:12
  • "because if the process can be killed like this then maybe the file can be half written when the process is killed" -- use SQLite, as it is transactional and so greatly reduces the likelihood of this occurrence (which is not all that likely to begin with). – CommonsWare Jul 13 '17 at 13:12
  • is it not a little too much heavy to add sqlite just for this ? – zeus Jul 13 '17 at 13:23

0 Answers0