7

I've implemented a sticky service and when I force close the app I see that the Activity's onDestroy() is called. But the service doesn't apparently do anything.


here's the logcat:

07-01 22:35:30.397: DEBUG/ActivityMine(6505): onDestroy()

07-01 22:35:32.667: INFO/ActivityManager(71): Force stopping package my.large.package uid=10036

07-01 22:35:32.667: WARN/ActivityManager(71): Scheduling restart of crashed service my.large.package/.service.ServiceCommunicator in 5000ms

07-01 22:35:32.667: INFO/Process(71): Sending signal. PID: 6505 SIG: 9

07-01 22:35:32.687: INFO/ActivityManager(71):   Force stopping service ServiceRecord{451f65b8 my.large.package/.service.ServiceCommunicator}

as you can see, ServiceCommunicator() doesn't call: stopService(), finalize(), or onDestroy() ! I've put Log calls in all 3 of them.

how do I know when the service is force closed? I need to trap that event so that I can close files.

Akram
  • 7,548
  • 8
  • 45
  • 72
Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166

3 Answers3

9

how do I know when the service is force closed?

You don't. onDestroy() is not guaranteed to be called, on any component.

I need to trap that event so that I can close files.

Then don't leave them open.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Instead of leaving files open, lets say that my Service.onCreate() shows a notification to indicate the service is active - processing data, etc. Since Service.onDestroy() isn't called, how can the notification be removed when the service is killed ? – Someone Somewhere Jul 05 '11 at 23:20
  • @Someone Somewhere: If it doesn't automatically go away, then you can't remove it until some piece of your code wakes up again and can `cancel()` it. Normally, a `Notification` is either going to be used for something short-lived (e.g., "we're downloading the file you requested") or something you flag with `startForeground()`. Moreover, if your service is being killed, you have left it running too long -- try to reorganize your implementation to use `AlarmManager` or other techniques to only have the service in memory occasionally, rather than continuously. – CommonsWare Jul 05 '11 at 23:25
  • it seems like 30mins to an hour whilst not in the foreground will get the service killed (even as a remote service). It does seem to run for a long time whilst in the foreground (i.e. overnight). Now that I know silent killing of a service is intended I can research it and hopefully work around it - it's just hard to work around when there's no event triggered as it's killed. – Someone Somewhere Jul 06 '11 at 18:00
  • startForeground() might be all I need – Someone Somewhere Jul 06 '11 at 18:19
  • 2
    It is very annoying to not get this notification because it seems to suggest that a service cannot cache ANY of its state if that state needs to persist past a shutdown. This in turn increases battery use (have to write to internal flash EVERY time state changes) and causes excessive writing which increase time to flash wear out. – Michael Mar 31 '13 at 20:45
  • 1
    @Michael thats my problem exactly. My service needs to cache state, but I only want to hit the disk if my service is being shutdown. Android can be quite the inconvenience. Also, sometimes we must run the service all the time. I am processing sensor data in real time, so running 'periodically' using a timer is not a solution for me. – Lo-Tan Aug 23 '14 at 23:17
0

Expecting a service to be force closed seems kinda weird to me. Anyways if the process is terminated i would expect any open file handle to be closed by the linux subsystem.

Also as stated by CommonsWare onDestroy isn't guaranteed in any case.

mibollma
  • 14,959
  • 6
  • 52
  • 69
-1

It's worse. onDestroy is only called in emulator environment (for testing), but never on a real phone....

And exactly as @mibollmba said - files will be closed when process dies. No need to handle file close.

Jarek Potiuk
  • 19,317
  • 2
  • 60
  • 61