force-closing an app with a service: onDestroy() i

2020-04-04 06:19发布

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.

标签: android
3条回答
家丑人穷心不美
2楼-- · 2020-04-04 06:40

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.

查看更多
Luminary・发光体
3楼-- · 2020-04-04 06:42

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.

查看更多
虎瘦雄心在
4楼-- · 2020-04-04 06:43

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.

查看更多
登录 后发表回答